【发布时间】: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