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

标签 python django django-models

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

Using ManyToMany through correctly for complex model in django

这里的结果是:

ipaswdb.ProviderLocations: (fields.E336) The model is used as an intermediate model by 'ipaswdb.Group.providers', but it does not have a foreign key to 'Group' or '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)

最佳答案

When you set up the intermediary model, you explicitly specify foreign keys to the models that are involved in the many-to-many relationship. This explicit declaration defines how the two models are related

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)

关于python - 没有外键错误的django中间模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003151/

相关文章:

python - sklearn "RidgeClassifier"是做什么的?

使用 session 时出现 Python pickle 错误

python - Django错误: TypeError: int() argument must be a string or a number,而不是 'BuildsTable'

python - Django:当我尝试从管理员创建用户时出现操作错误

python - Django中如何过滤相关对象的相关对象?

python - 如何记录 Python 程序执行的 CPU 指令?

python - 如何使用python将F2键发送到远程主机

python - 如何为pytest命令指定几个标记

python - 基于 Django 类的 DetailView/ListView ajax 装饰器?

sql - Postgresql 转储是否创建以最后一个键开头或之后的序列?