没有外键错误的django中间模型

Cod*_*joy 5 python django django-models

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

在 django 中为复杂模型正确使用 ManyToMany

结果在这里得到了这么远:

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
Run Code Online (Sandbox Code Playgroud)

提供者 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)
Run Code Online (Sandbox Code Playgroud)

我也尝试对 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)
Run Code Online (Sandbox Code Playgroud)

Mos*_*oye 2

当您设置中介模型时,您可以显式指定多对多关系中涉及的模型的外键。这个显式声明定义了两个模型如何相关

Group(不是GroupLocations) 和Provider是 M2M 关系中的,因此 throughmodelProviderLocations必须链接到它们ForeignKeys

class ProviderLocations(models.Model):
    provider = models.ForeignKey('Provider', on_delete=models.CASCADE)
    group = models.ForeignKey('Group', on_delete=models.CASCADE)
Run Code Online (Sandbox Code Playgroud)

事实上,每个提供者都有一组组,每个组都有一组组位置,您可以遍历该层次结构来获取您的对象。

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)
Run Code Online (Sandbox Code Playgroud)