Django Admin高级开发:多层级内联嵌套包nested-admin

2023-11-0816:40:46后端程序开发Comments970 views字数 1722阅读模式

Django-nested-admin是一个可以嵌套admin内联的项目(即在InlineModelAdmin类上定义内联)。Django admin自带两种嵌套方式TabularInline和StackedInline,用于在父模型的编辑页面中以表格形式显示关联的子模型对象。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

一、安装文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

pip install django-nested-admin

二、配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

  1. setting.py添加如下
INSTALLED_APPS = (    # ...    'nested_admin',)

2.配置url.py文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

urlpatterns = [    # ...    path('_nested_admin/', include('nested_admin.urls')),]

3.说明文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

使用django-nested-admin,请使用以下类代替它们的django admin原有的model方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

django.contrib.adminnested_admin
ModelAdminNestedModelAdmin
InlineModelAdminNestedInlineModelAdmin
StackedInlineNestedStackedInline
TabularInlineNestedTabularInline

三、实例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

使用django-nested-admin实现三级嵌套文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

models.py文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

class Choice(models.Model):    #投票选项表    question = models.ForeignKey(Question, on_delete=models.CASCADE,editable=False,related_name='q_choice', )    choice_text = models.CharField("选项内容",max_length=200)    img = models.ImageField("选项图", upload_to="media/polls/",null=True,blank=True)    votes = models.IntegerField("投票数",default=0,editable=False)    ctime = models.DateTimeField("创建时间", auto_now_add=True, blank=True, max_length=20)    def __str__(self):        return self.choice_text    class Meta:        db_table = "choice"        verbose_name = "投票选项"        verbose_name_plural = verbose_name

admin.py文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

import nested_admin
class ChoiceInline(nested_admin.NestedTabularInline):    model = Choice    extra =4    exclude = ('question','question_id',)    readonly_fields = ('votes',)  class QuestionInline(nested_admin.NestedStackedInline):    model = Question    inlines = [ChoiceInline]    extra = 1     @admin.register(Polls)class PollsAdmin(nested_admin.NestedModelAdmin):    # form = PollsForm    list_display =['ordinal_number','title','questions','starttime','endtime','view','max','analysis','ctime']    list_display_links = ('title',)    inlines = [QuestionInline]

最终运行效果:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

Django Admin高级开发:多层级内联嵌套包nested-admin文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

可以实现三级嵌套并动态添加文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

github:https://github.com/theatlantic/django-nested-admin官方文档:https://django-nested-admin.readthedocs.io/en/latest/

菲宇 Python与Django学习文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/56843.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/56843.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定