管理员模型中缺少外键?

时间:2019-01-28 14:14:37

标签: django foreign-keys relation

尝试更新数据库时出现以下错误:

class'artdb.admin.RoleInline':(admin.E202)'artdb.Role'没有对'artdb.Person'的ForeignKey

我希望人与角色之间存在多对多的关系

model.py(不显示所有类):

class Person(models.Model):

    mail=models.EmailField()
    firstName=models.CharField(max_length=200)
    lastName=models.CharField(max_length=200)
    phoneNumber=PhoneNumberField()
    streetAdress=models.CharField(max_length=200)
    zipcode=models.CharField(max_length=200)
    city=models.CharField(max_length=200,default="Göteborg")
    country=models.CharField(max_length=200,default="Sweden")

    def __str__(self):
        return "%s %s" % (self.firstName,self.lastName)

    class Meta:
        ordering = ('firstName','lastName')

class Role(models.Model):

    role=models.CharField(max_length=200)
    person=models.ManyToManyField(Person)

    def __str__(self):
        return self.role

    class Meta:
        ordering = ('role',)

admin.py(未显示所有类):

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 PersonInline(admin.ModelAdmin):

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

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

它以前与运行manage.py makemigrations artdb一起使用 我看不到模型中的错误。

1 个答案:

答案 0 :(得分:1)

根据ManytoMany relationship in TabularInline的文档,您对RoleInline的模型有误

 model = Role.persons.through