首页 后端开发 Python教程 Djangoadminsite(三)InlineModelAdmin

Djangoadminsite(三)InlineModelAdmin

Dec 23, 2016 pm 05:46 PM

InlineModelAdmin

class InlineModelAdmin
class TabularInline
class StackedInline

举例,有两个Model:

from django.db import models

class Author(models.Model):
  name = models.CharField(max_length=100)

class Book(models.Model):
  author = models.ForeignKey(Author)
  title = models.CharField(max_length=100)

如果想在Author页面上编辑book:

from django.contrib import admin

class BookInline(admin.TabularInline):
   model = Book

class AuthorAdmin(admin.ModelAdmin):
   inlines = [
       BookInline,
   ]

Django提供了两个InlineModelAdmin的子类:

TabularInline
StackedInline
区别在于使用的模板。

InlineModelAdmin options选项

InlineModelAdmin和ModelAdmin共通的options有:

form
fieldsets
fields
formfield_overrides
exclude
filter_horizontal
filter_vertical
ordering
PRepopulated_fields
get_queryset()
radio_fields
readonly_fields
raw_id_fields
formfield_for_choice_field()
formfield_for_foreignkey()
formfield_for_manytomany()
has_add_permission()
has_change_permission()
has_delete_permission()

额外增加的options有:

InlineModelAdmin.model
inline使用的model,必需。

InlineModelAdmin.fk_name
model的name,当有多个外键时使用。

InlineModelAdmin.formset
缺省BaseInlineFormSet。

InlineModelAdmin.form
缺省ModelForm。当创建formset时传递给inlineformset_factory()。

InlineModelAdmin.extra
inline的额外数目。

InlineModelAdmin.get_extra()也返回inline的额外数目。

InlineModelAdmin.max_num
可展示得最大数目。

InlineModelAdmin.get_max_num()也返回此数字。

InlineModelAdmin.min_num
可展示的最小数目。

InlineModelAdmin.get_min_num()也返回此数目。

InlineModelAdmin.raw_id_fields
同ModelAdmin。

class BookInline(admin.TabularInline):
   model = Book
   raw_id_fields = ("pages",)

InlineModelAdmin.template
使用的模板。

InlineModelAdmin.verbose_name
覆盖meta class中的verbose_name。

InlineModelAdmin.verbose_name_plural
同上

InlineModelAdmin.can_delete
默认为True。

InlineModelAdmin.get_formset(request, obj=None, **kwargs)
参考ModelAdmin.get_formsets_with_inlines.

InlineModelAdmin.get_extra(request, obj=None, **kwargs)

class BinaryTreeAdmin(admin.TabularInline):
   model = BinaryTree

   def get_extra(self, request, obj=None, **kwargs):
       extra = 2
       if obj:
           return extra - obj.binarytree_set.count()
       return extra

InlineModelAdmin.get_max_num(request, obj=None, **kwargs)

class BinaryTreeAdmin(admin.TabularInline):
   model = BinaryTree

   def get_max_num(self, request, obj=None, **kwargs):
       max_num = 10
       if obj.parent:
           return max_num - 5
       return max_num

InlineModelAdmin.get_min_num(request, obj=None, **kwargs)
见上。

多个 ForeignKey链向同一个Model的情况

如果有多个foreign keys:

from django.db import models

class Friendship(models.Model):
   to_person = models.ForeignKey(Person, related_name="friends")
   from_person = models.ForeignKey(Person, related_name="from_friends")

展示其中一个:

from django.contrib import admin
from myapp.models import Friendship

class FriendshipInline(admin.TabularInline):
   model = Friendship
   fk_name = "to_person"

class PersonAdmin(admin.ModelAdmin):
   inlines = [
       FriendshipInline,
   ]

Working with many-to-many models

model例子:

from django.db import models

class Person(models.Model):
   name = models.CharField(max_length=128)

class Group(models.Model):
   name = models.CharField(max_length=128)
   members = models.ManyToManyField(Person, related_name='groups')

inlines展示:

from django.contrib import admin

class MembershipInline(admin.TabularInline):
   model = Group.members.through

class PersonAdmin(admin.ModelAdmin):
   inlines = [
       MembershipInline,
   ]

class GroupAdmin(admin.ModelAdmin):
   inlines = [
       MembershipInline,
   ]
   exclude = ('members',)

Note:

第一 , the MembershipInline类指向Group.members.through. The through attribute指向管理the many-to-many关系的数据库. 

第二,, the GroupAdmin 必须排除the members字段.

Working with many-to-many intermediary models

明确指出中间model的例子:

from django.db import models

class Person(models.Model):
   name = models.CharField(max_length=128)

class Group(models.Model):
   name = models.CharField(max_length=128)
   members = models.ManyToManyField(Person, through='Membership')

class Membership(models.Model):
   person = models.ForeignKey(Person)
   group = models.ForeignKey(Group)
   date_joined = models.DateField()
   invite_reason = models.CharField(max_length=64)

第一步:

class MembershipInline(admin.TabularInline):
   model = Membership
   extra = 1

第二步:

class PersonAdmin(admin.ModelAdmin):
   inlines = (MembershipInline,)

class GroupAdmin(admin.ModelAdmin):
   inlines = (MembershipInline,)

第三步:

admin.site.register(Person, PersonAdmin)
admin.site.register(Group, GroupAdmin)

Using generic relations as an inline

一个inline with generically related objects例子:

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey

class Image(models.Model):
   image = models.ImageField(upload_to="images")
   content_type = models.ForeignKey(ContentType)
   object_id = models.PositiveIntegerField()
   content_object = GenericForeignKey("content_type", "object_id")

class Product(models.Model):
   name = models.CharField(max_length=100)

如果想在Product add/change页面上编辑Image实例,可以使用GenericTabularInline或GenericStackedInline:

from django.contrib import admin
from django.contrib.contenttypes.admin import GenericTabularInline

from myproject.myapp.models import Image, Product

class ImageInline(GenericTabularInline):
   model = Image

class ProductAdmin(admin.ModelAdmin):
   inlines = [
       ImageInline,
   ]

admin.site.register(Product, ProductAdmin)

 以上就是Djangoadminsite(三)InlineModelAdmin的内容,更多相关内容请关注PHP中文网(www.php.cn)!


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1666
14
CakePHP 教程
1425
52
Laravel 教程
1325
25
PHP教程
1273
29
C# 教程
1252
24
Python vs.C:申请和用例 Python vs.C:申请和用例 Apr 12, 2025 am 12:01 AM

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

Python:游戏,Guis等 Python:游戏,Guis等 Apr 13, 2025 am 12:14 AM

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

Python与C:学习曲线和易用性 Python与C:学习曲线和易用性 Apr 19, 2025 am 12:20 AM

Python更易学且易用,C 则更强大但复杂。1.Python语法简洁,适合初学者,动态类型和自动内存管理使其易用,但可能导致运行时错误。2.C 提供低级控制和高级特性,适合高性能应用,但学习门槛高,需手动管理内存和类型安全。

Python和时间:充分利用您的学习时间 Python和时间:充分利用您的学习时间 Apr 14, 2025 am 12:02 AM

要在有限的时间内最大化学习Python的效率,可以使用Python的datetime、time和schedule模块。1.datetime模块用于记录和规划学习时间。2.time模块帮助设置学习和休息时间。3.schedule模块自动化安排每周学习任务。

Python vs.C:探索性能和效率 Python vs.C:探索性能和效率 Apr 18, 2025 am 12:20 AM

Python在开发效率上优于C ,但C 在执行性能上更高。1.Python的简洁语法和丰富库提高开发效率。2.C 的编译型特性和硬件控制提升执行性能。选择时需根据项目需求权衡开发速度与执行效率。

Python标准库的哪一部分是:列表或数组? Python标准库的哪一部分是:列表或数组? Apr 27, 2025 am 12:03 AM

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

Python:自动化,脚本和任务管理 Python:自动化,脚本和任务管理 Apr 16, 2025 am 12:14 AM

Python在自动化、脚本编写和任务管理中表现出色。1)自动化:通过标准库如os、shutil实现文件备份。2)脚本编写:使用psutil库监控系统资源。3)任务管理:利用schedule库调度任务。Python的易用性和丰富库支持使其在这些领域中成为首选工具。

学习Python:2小时的每日学习是否足够? 学习Python:2小时的每日学习是否足够? Apr 18, 2025 am 12:22 AM

每天学习Python两个小时是否足够?这取决于你的目标和学习方法。1)制定清晰的学习计划,2)选择合适的学习资源和方法,3)动手实践和复习巩固,可以在这段时间内逐步掌握Python的基本知识和高级功能。

See all articles