Django嵌套内联,有什么解决方法?

时间:2018-08-30 11:21:31

标签: python django nested inline

我想在admin中编辑一个模型,该模型具有另一个模型的外键,而另一个模型又具有基础模型的外键:

from django.contrib import admin
from .models import Role
from .models import Address
from .models import Date
from .models import Person
from .models import Name

# Register your models here.

admin.site.register(Role)
admin.site.register(Address)
admin.site.register(Date)
#admin.site.register(Name)
#admin.site.register(Person)

class RoleInline(admin.TabularInline):

    model=Role
    extra=3

class NameInline(admin.TabularInline):

    model=Name
    extra=3

class PersonInline(admin.ModelAdmin):

    fieldsets=[
        (None,{'fields': ['mail','firstName','lastName','phoneNumber','streetAdress','zipcode','city']}),
    ]
    inlines = [RoleInline,Name]
    search_fields = ['firstName']

#admin.site.register(Name,NameInline)
admin.site.register(Person,PersonInline)

这是模型:

class Role(models.Model):

    role=models.CharField(max_length=200)
    person=models.ForeignKey(Person,on_delete=models.CASCADE)

    def __str__(self):
        return self.role

    class Meta:
        ordering = ('role',)

class Name(models.Model):

    name=models.CharField(max_length=200)
    role=models.ForeignKey(Role,on_delete=models.CASCADE)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ('name',)

class Address(models.Model):

    name=models.ForeignKey(Name,on_delete=models.CASCADE)
    street=models.CharField(max_length=200)
    city=models.CharField(max_length=200)
    mail=models.EmailField()
    phone=PhoneNumberField()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "addreses"

    class Meta:
        ordering = ('name',)

class Date(models.Model):

    date=models.DateField()
    location=models.ManyToManyField(Address)

问题是名称与角色有关,而不与人直接相关,即角色对人具有外键,名称对角色具有外键,必须将其添加到我正在使用的管理页面中。 django中缺少嵌套的内联要素,有什么解决方法吗?

0 个答案:

没有答案