Django admin管理工具TabularInline类用法详解
作者:ZeroChia 时间:2021-05-10 16:16:26
TabularInline类用于在A页面上编辑B表的字段,举例说明,如果models.py中有Staff、Job两个model:
from django.db import models
class Staff(models.Model):
name = models.CharField(max_length=10)
class Job(models.Model):
staff = models.ForeignKey(Staff)
task = models.CharField(max_length=100)
如果想在Staff页面编辑Job的内容,可以在admin.py中这么写:
from django.contrib import admin
class JobInline(admin.TabularInline):
model = Job
class StaffAdmin(admin.ModelAdmin):
inlines = [
JobInline,
]
如果想编辑第三个表,也可以直接加上,比如models.py中职员还有一个工资表Salary:
class Salary(models.Model):
staff = models.ForeignKey(Staff)
salary = models.IntegerField(max_length=8)
可以在admin.py中这么写:
from django.contrib import admin
class JobInline(admin.TabularInline):
model = Job
class SalaryInline(admin.TabularInline):
model = Salary
class StaffAdmin(admin.ModelAdmin):
inlines = [
JobInline,
SalaryInline
]
Django提供了两个InlineModelAdmin的子类,分别是TabularInline和StackedInline,区别在于使用的模板不同。
补充知识:django admin TabularInline raw_id_fields 添加查询 搜索小图标显示
问题:在django app admin TabularInline类使用了下面引用了 raw_id_fields 还是找不到搜索小图标。模型引用的是在别的app的model下。
解决方案:yourapp.adminx.py 下面需要注册对应的模型。
来源:https://blog.csdn.net/ZeroChia/article/details/82423765
标签:Django,admin,TabularInline
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Pycharm 操作Django Model的简单运用方法
2022-05-09 14:07:15
![](https://img.aspxhome.com/file/2023/9/110259_0s.jpg)
Python手机与电脑游戏脚本的编写方法
2023-11-28 18:31:44
![](https://img.aspxhome.com/file/2023/7/92217_0s.gif)
asp详解session的用法
2007-09-07 10:22:00
python包合集shutil示例代码详解
2022-03-28 12:04:27
![](https://img.aspxhome.com/file/2023/5/115755_0s.png)
Web2.0视觉风格进化论 之一
2007-11-03 19:58:00
![](https://img.aspxhome.com/file/UploadPic/up/2007110320250569.gif)
Python设计模式之单例模式实例
2023-06-02 12:43:02
python 限制函数调用次数的实例讲解
2023-11-11 00:34:23
如何编写一个过滤掉HTML代码的函数?
2009-11-08 18:53:00
Python学习之yaml文件的读取详解
2023-04-20 14:06:42
![](https://img.aspxhome.com/file/2023/1/109591_0s.jpg)
python矩阵的基本运算及各种操作
2023-01-12 11:37:27
![](https://img.aspxhome.com/file/2023/5/111925_0s.png)
python画图常规设置方式
2023-11-25 03:52:04
![](https://img.aspxhome.com/file/2023/9/130799_0s.jpg)
python中subplot大小的设置步骤
2021-07-30 08:59:37
![](https://img.aspxhome.com/file/2023/2/67312_0s.png)
python面向对象多线程爬虫爬取搜狐页面的实例代码
2021-11-27 21:49:39
![](https://img.aspxhome.com/file/2023/5/120425_0s.png)
SQL Server中的执行引擎入门 图解
2012-06-06 20:08:26
深度解析Django REST Framework 批量操作
2021-05-18 03:03:21
![](https://img.aspxhome.com/file/2023/2/76002_0s.png)
4个的Python自动化脚本分享
2021-05-28 19:22:29
利用Python为iOS10生成图标和截屏
2021-05-03 11:15:09
Check In和Check Out的多人协作管理
2007-02-03 11:39:00
交互设计实用指南系列(7)–避免迷路
2010-01-23 09:52:00
![](https://img.aspxhome.com/file/UploadPic/20101/23/34-62s.jpg)
python使用arcpy.mapping模块批量出图
2021-03-12 04:28:15