I need a nested django admin inline, which I can include the date field inlines in an other inline like below.
I have the models below:
class Person(models.Model):
name = models.CharField(max_length=200)
id_no = models.IntegerField()
class Certificate(models.Model):
cerfificate_no = models.CharField(max_length=200)
certificate_date = models.DateField(max_length=100)
person = models.ForeignKey(Person)
training = models.CharField(max_length=200)
class Training_Date(models.Model):
date = models.DateField()
certificate = models.ForeignKey(Certificate)
And, the admin below:
class CertificateInline(admin.StackedInline):
model = Certificate
class PersonAdmin(admin.ModelAdmin):
inlines = [CertificateInline,]
admin.site.register(Person,PersonAdmin)
But, I need to include the Training_Date model as inline which is part of Certificate admin inline.
Any idea?
8条答案
按热度按时间q7solyqu1#
There has been some movement in https://code.djangoproject.com/ticket/9025 recently, but I wouldn't hold my breath.
One common way around this is to link to an admin between first and second (or second and third) level by having both a
ModelAdmin
and an Inline for the same model:Give
Certificate
aModelAdmin
withTrainingDate
as an inline. Setshow_change_link = True
forCertificateInline
so you can click on an inline to go to itsModelAdmin
change form.admin.py:
2skhul332#
更通用的解决方案
zpqajqem3#
这个软件包应该能满足你的需要。
xu3bshqb4#
AFAIK,你不能在默认的Django管理中有第二级内联。
Django admin只是一个普通的Django应用程序,所以没有什么可以阻止你实现第二层嵌套表单,但恕我直言,这将是一种复杂的设计,也许这就是为什么没有提供它的原因。
gblwokeq5#
嵌套内联位于:https://github.com/BertrandBordage/django-super-inlines/
x33g5p2x6#
A more up to date solution (february 2021) is to use the show_change_link config variable: https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.show_change_link
This does exactly the same as the EditLinkToInlineObject proposed in solutions above, but is less code and is probably well tested by Django Developers
You would just have to define
show_change_link=True
in each one of your inlinesUPDATE (January 25th, 2022): Here's the updated link in the docs (Django 4.0): https://docs.djangoproject.com/en/4.0/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.show_change_link
kxxlusnw7#
使用django-nested-admin,这是最好的内嵌包。
首先,安装**“django-nested-admin”**:
然后,将**“nested_admin”添加到”www.example.com“中的“已安装应用程序”settings.py:
然后,将**“路径('_nested_ad..."添加到”www.example.com“中的“网址模式”urls.py:
最后,在**”www.example.com“中使用“Training_DateInline()”和“CertificateInline()”类扩展“嵌套表格内联”,并使用“PersonAdmin()”类扩展“嵌套模型管理”admin.py,如下所示:
ccrfmcuu8#
我使用了@bigzbig提供的解决方案(谢谢)。
我还想回到第一个列表页面,一旦更改已保存,所以添加: