【问题标题】:django intermediate model with no foreign key error没有外键错误的django中间模型
【发布时间】:2016-06-23 22:49:43
【问题描述】:

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

Using ManyToMany through correctly for complex model in django

结果到这里为止:

ipaswdb.ProviderLocations: (fields.E336) 模型用作 'ipaswdb.Group.providers' 的中间模型,但它没有 'Group' 或 'Provider' 的外键。

我的意思是,错误是英文,但我不知道它真正想告诉我什么。

下面的完整模型,但想法是让 A 组有许多位置。

一个提供者通过他们工作的任何位置属于一个组,其次,一个提供者可以属于多个位置的多个组

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)

【问题讨论】:

    标签: python django django-models


    【解决方案1】:

    当您设置中间模型时,您明确指定外部 多对多关系中涉及的模型的关键。 这个明确的声明定义了两个模型是如何相关的

    Group(不是GroupLocations)和Provider是M2M关系中的那些,所以直通模型ProviderLocations必须链接到它们的ForeignKeys

    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 的正确方法,因此会出现错误。
    • 我不想让它在 cmets 中拖后腿,但是在再次进入这个项目并发现管理员没有正确显示这种关系之后。例如,组中的提供者不会显示为字段。此外,组位置需要(我知道如何解决)提供者,但如果有新组位置的提供者,我必须将整个另一个组位置添加到数据库。这似乎不正确。我确实有另一个在管理界面中完美显示的多对多模型。有一个漂亮的列表框多项选择等不知道我错过了什么。
    • 虽然这里的答案确实解决了外键错误的话题。
    • @Codejoy 好吧,如果你认为它解决了手头的问题,你可以考虑接受它
    猜你喜欢
    • 2020-04-17
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2018-07-02
    • 2015-03-07
    相关资源
    最近更新 更多