Django Admin中的嵌套内联

时间:2014-03-24 11:06:21

标签: python django django-models django-admin

我有以下场景,其中有三个模型,应该嵌套在DJango admin中。我在https://github.com/Soaa-/django-nested-inlines

中使用了Django 1.6版本和应用的设置

但是,它没有达到预期的输出。是否有任何其他解决方案在Django中实现嵌套的内联。我是这个框架的新手。请指导我解决这个问题。

model.py

class Project(models.Model):
    name = models.CharField(max_length=200)
    code = models.IntegerField(default=0)
    def __unicode__(self):
        return self.name

class Detail(models.Model):
    project = models.ForeignKey(Project)
    value = models.DecimalField(max_digits=5, decimal_places=2)
    location = models.IntegerField(default=0)

class Configuration(models.Model):
    detail = models.OneToOneField(Detail)
    content1 = models.IntegerField()
    content2 = models.IntegerField()

admin.py

from django.contrib import admin
from nested_inlines.admin import NestedModelAdmin, NestedTabularInline, NestedStackedInline

from myapp.models import Project, Detail, Configuration

class ConfigInline(NestedStackedInline):
    model = Configuration
    extra = 1

class DetailInline(NestedTabularInline):
    model = Detail
    inlines = [ConfigInline,]
    extra = 1

class ProjectAdmin(admin.ModelAdmin):
    inlines = [DetailInline]

admin.site.register(Project, ProjectAdmin)

3 个答案:

答案 0 :(得分:1)

尝试https://pypi.python.org/pypi/django-nested-inline

它已更新为与Django 1.6一起使用

答案 1 :(得分:1)

我相信您已忘记将ProjectAdmin设置为NestedModelAdmin

admin.py:

from django.contrib import admin
from nested_inlines.admin import NestedModelAdmin, NestedTabularInline, NestedStackedInline

from myapp.models import Project, Detail, Configuration

class ConfigInline(NestedStackedInline):
    model = Configuration
    extra = 1

class DetailInline(NestedTabularInline):
    model = Detail
    inlines = [ConfigInline,]
    extra = 1

class ProjectAdmin(NestedModelAdmin):
    inlines = [DetailInline]

admin.site.register(Project, ProjectAdmin)

答案 2 :(得分:0)

在你导入下面这个包之前先安装这个

<块引用>

pip install django-nested-admin 然后将其添加到已安装的应用列表中

INSTALLED_APPS = [
   ...
   'nested_admin',
   ...
]

现在,您可以导入它了。

from nested_inline.admin import NestedModelAdmin, NestedTabularInline, NestedStackedInline