简体   繁体   English

在Django Admin中显示和添加孙子代

[英]Display & Add Grandchildren in Django Admin

We have 3 tables defined in models.py: 我们在models.py中定义了3个表:


class country(models.Model):
    country_id = models.AutoField(primary_key=True)
    country_name = models.CharField(max_length=100)

class region(models.Model): country = models.ForeignKey(country) region_id = models.AutoField(primary_key=True) region_name = models.CharField(max_length=100)

class school(models.Model): region = models.ForeignKey(region) school_id = models.AutoField(primary_key=True) school_name = models.CharField(max_length=100)

We would like to be able to add a country, then in the same admin screen, add a region, then add a school within that new region. 我们希望能够添加一个国家,然后在同一管理屏幕中添加一个区域,然后在该新区域内添加学校。 All fairly basic stuff and doing the first step of adding a country then a region can be done by adding these lines to admin.py: 通过将以下几行添加到admin.py中,可以完成所有相当基本的工作,并先完成添加国家/地区的第一步:


class RegionInline(admin.StackedInline):
    model = region
    extra = 1

class CountryAdmin(admin.ModelAdmin): inlines = [RegionInline]

admin.site.register(country, CountryAdmin)

Unfortunately we cannot work out how to even display schools in the same screen as adding/editing a country. 不幸的是,我们甚至无法在添加/编辑国家/地区的同一屏幕上显示学校。

We have also considered looking at it from the perspective of the schools and having the country and region inline, but have the same basic problem of not being able to display the country because it is not directly linked to the school. 我们也考虑过从学校的角度看待这个问题,并且使国家和地区内联,但是由于它与学校没有直接联系,因此也存在无法显示国家的基本问题。

So, in a nutshell, our question is: 简而言之,我们的问题是:

Using the Django admin process how are records displayed & added that are not directly linked to the current model? 使用Django管理流程,如何显示和添加未直接链接到当前模型的记录?

We very much suspect that the answer to this question is explained somewhere in the great Django documentation but we are new to Python/Django and can't find it, so if anyone can point us in the right direction then that would be great and save re-inventing the wheel. 我们非常怀疑这个问题的答案是在伟大的Django文档中的某个地方解释的,但是我们是Python / Django的新手,并且找不到它,因此,如果有人可以将我们指向正确的方向,那将是很好并且可以节省重新发明轮子。

Python v2.7, Django v1.7.5 Python v2.7,Django v1.7.5

Current version of django doesn't support third level of inlines in admin. 当前版本的django在admin 中不支持内联的第三级。

You have to use the django-nested-inline app. 您必须使用django-nested-inline应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM