django中间模型没有外键错误

时间:2016-06-23 22:49:43

标签: python django django-models

为我的django应用程序构建一些复杂的模型并在这里得到一些帮助:

Using ManyToMany through correctly for complex model in django

结果在这里得到了很多:

  

ipaswdb.ProviderLocations:(fields.E336)该模型用作   中间模型由' ipaswdb.Group.providers',但它没有   “' Group'的外键或者'提供商'。

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

以下完整模型,但我们的想法是让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)

1 个答案:

答案 0 :(得分: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)
相关问题