Python 无外键错误的django中间模型

Python 无外键错误的django中间模型,python,django,django-models,Python,Django,Django Models,为我的django应用程序构建一些复杂模型,并在此处获得了一些帮助: 其结果是在这里走了这么远: ipaswdb.ProviderLocations:(fields.E336)模型用作 由“ipaswdb.Group.providers”创建的中间模型,但它没有 “组”或“提供程序”的外键 我的意思是,是的,错误是英语,但我不知道它到底想告诉我什么 下面是完整的模型,但想法是让一个团队拥有多个地点 一个提供者通过其工作地点属于一个组,其次,一个提供者可以在多个地点属于多个组 Provider

为我的django应用程序构建一些复杂模型,并在此处获得了一些帮助:

其结果是在这里走了这么远:

ipaswdb.ProviderLocations:(fields.E336)模型用作 由“ipaswdb.Group.providers”创建的中间模型,但它没有 “组”或“提供程序”的外键

我的意思是,是的,错误是英语,但我不知道它到底想告诉我什么

下面是完整的模型,但想法是让一个团队拥有多个地点

一个提供者通过其工作地点属于一个组,其次,一个提供者可以在多个地点属于多个组

Provider A -> location 1
         -> location 2
         -> location 3

Group 1 -> has a location 1
        -> has a location 2

Group 2 -> has a location 3
        -> has a location 4
提供商A通过位置1、2、3属于第1组和第2组

我的全部型号是:

class Group(models.Model):
    group_name = models.CharField(max_length=50)

    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)
    providers = models.ManyToManyField('Provider', through='ProviderLocations')




class Provider(models.Model):
    first_name = models.CharField(max_length = 50)

    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)



class ProviderLocations(models.Model):
    provider = models.ForeignKey('Provider', on_delete=models.CASCADE)
    group_location = models.ForeignKey('GroupLocations', on_delete=models.CASCADE)
    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)

class GroupLocations(models.Model):
    address = models.ForeignKey('Address', on_delete= models.SET_NULL, null=True)
    group = models.ForeignKey('Group', on_delete=models.SET_NULL, null=True)
    doing_business_as = models.CharField(max_length = 255)
    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)
我也尝试过对ProviderLocations进行编辑,但没有结果:

class ProviderLocations(models.Model):
    provider = models.ForeignKey('Provider', on_delete=models.CASCADE)
    group = models.ManyToManyField('Group', through='GroupLocations')
    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)
设置中介模型时,需要显式指定“外部” 多对多关系中涉及的模型的关键点。 此显式声明定义了这两个模型的关联方式

Group
(不是
GroupLocations
)和
Provider
是M2M关系中的两个,因此直通模型
Provider位置必须链接到它们的
外键

class ProviderLocations(models.Model):
    provider = models.ForeignKey('Provider', on_delete=models.CASCADE)
    group = models.ForeignKey('Group', on_delete=models.CASCADE)
按原样,每个提供者都有一组组,每个组都有一组组位置,您可以遍历该层次结构来获取对象

通过消除
ProviderLocations
模型并使用
GroupLocations
作为直通模型,您可以通过组位置使提供者成为组的一部分:

class Group(models.Model):
    group_name = models.CharField(max_length=50)

    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)
    providers = models.ManyToManyField('Provider', through='GroupLocations')


class Provider(models.Model):
    first_name = models.CharField(max_length = 50)

    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)


class GroupLocations(models.Model):
    provider = models.ForeignKey('Provider', on_delete=models.CASCADE)
    group = models.ForeignKey('Group', on_delete=models.SET_NULL, null=True)
    address = models.ForeignKey('Address', on_delete= models.SET_NULL, null=True)
    doing_business_as = models.CharField(max_length = 255)
    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)

对我来说,这并不能说明提供者是通过组位置的组的一部分。但就你所拥有的而言,这不是实现throughmodel的正确方法,因此出现了错误。我不想在评论中继续这样做,但在再次访问该项目并发现管理员没有正确显示这种关系之后。例如,组中的提供者不会显示为字段。此外,组位置需要(我知道如何修复)一个提供程序,但是如果有一个新组位置的提供程序,我必须向DB添加另一个组位置。这似乎不正确。我确实有另一款多对多的模型,它在管理界面上显示得非常完美。用一个漂亮的列表框多选择ETC不确定我丢失了哪一块。虽然这里的答案确实修复了外键错误的主题。“CordyJoy,如果你认为它修复了手头的问题,你可以考虑接受它。