问题描述
我的django应用程序中有一个半复杂的模型关系。我有一个可以有很多位置的组(一个业务)
所以
一个组可以有很多提供者(人)。事实是,提供者通过组的位置连接到一个特定的组。也就是说,提供商可以拥有该位置。实际上,我相信提供者可以拥有多个位置(属于多个组)。我在django到目前为止,我不认为正确的方式是这样的:
class GroupLocations(models。模型):
address_id = models.ForeignKey(Address,on_delete = models.SET_NULL)
group_id = models.ForeignKey(Group,on_delete = models.SET_NULL)
doing_business_as = models.CharField(max_length = 255)
created_at = models.DateField(auto_now = false,auto_now_add = true)
updated_at = models.DateField(auto_now = true,auto_now_add = true)
class ProviderLocations model.Model):
provider_id = models.ForeignKey(Provider,on_delete = models.CASCADE)
group_location_id = models.ForeignKey(GroupLocations,on_delete = models.CASCADE)
created_at = models.DateField (auto_now = false,auto_now_add = true)
updated_at = models.DateField(auto_now = true,auto_now_add = true)
我的问题是,我的组(和/或提供者)模型是否需要具有某种类型的关系他们的模型定义?
类组(models.Model):
group_name = models.CharField(max_length = 50)
group_contact = models.CharField(max_length = 50)
#do我需要以下内容:
providers = models.ManyToMany(Provider,through ='ProviderLocations')
provider_locations = models.ManyToMany(Group,through ='GroupLocations'
class Provider(models.Model):
created_at = models.DateField(auto_now = false,auto_now_add = true)
updated_at = models.DateField(auto_now = true,auto_now_add = true)
groups = models.ManyToManyField(Group,through ='GroupLocations')
group_locations = models.ManyToMany(GroupLocations,through = 'ProviderLocations')
这是我可以从提供者获取一组组,而组位置
,我可以从一个组和提供商的位置获取一个供应商的列表。实际上更像是加入的位置ich到哪里我仍然在学习Django'ss关系系统,所以对如何使这些关系能很好地协同工作的任何建设性的批评将是有帮助的。
资料来源:
因此,您的模型可以被简化。
class Group(models.Model):
group_name = models.CharField(max_length = 50)
group_contact = models.CharField(max_length = 50)
providers = models。 ManyToMany(Provider,through ='ProviderLocations')
class Provider(models.M odel)
created_at = models.DateField(auto_now = false,auto_now_add = true)
updated_at = models.DateField(auto_now = true,auto_now_add = true)
我不太确定为什么要尝试创建一个 GroupLocation
和 ProviderLocation
模型。我相信他们可以合并。
I have a semi complex model relationship in my django app.
I have a Group (a business) which can have many locationsSo
A group can have many providers (person) as well. The thing is, the Provider is connected to a particular group through the group location. That is to say a provider can have that location. In reality, i believe a provider can have many locations (belonging to multiple groups). The way I have this in my django so far which I don't think correct is this way:
class GroupLocations(models.Model):
address_id = models.ForeignKey(Address, on_delete= models.SET_NULL)
group_id = models.ForeignKey(Group, on_delete=models.SET_NULL)
doing_business_as = models.CharField(max_length = 255)
created_at=models.DateField(auto_now=false, auto_now_add=true)
updated_at=models.DateField(auto_now=true, auto_now_add=true)
class ProviderLocations(models.Model):
provider_id = models.ForeignKey(Provider, on_delete=models.CASCADE)
group_location_id = models.ForeignKey(GroupLocations, on_delete=models.CASCADE)
created_at=models.DateField(auto_now=false, auto_now_add=true)
updated_at=models.DateField(auto_now=true, auto_now_add=true)
My question is, does my Group (and/or Provider) model need to have some sort of relationship specified in their model definitions?
class Group(models.Model):
group_name = models.CharField(max_length=50)
group_contact= models.CharField(max_length=50)
#do I need something like the following:
providers = models.ManyToMany(Provider, through='ProviderLocations')
provider_locations = models.ManyToMany(Group, through='GroupLocations'
class Provider(models.Model):
created_at=models.DateField(auto_now=false, auto_now_add=true)
updated_at=models.DateField(auto_now=true, auto_now_add=true)
groups = models.ManyToManyField(Group, through='GroupLocations')
group_locations = models.ManyToMany(GroupLocations, through='ProviderLocations')
This is so i can get a list of groups from a provider, and the groups locationsand I can get a list of providers from a group and the providers locations.Actually more like the locations join which to which. I am still learning Django'ss relationship systems so any constructive criticism of how to make these relationships work well together would be helpful.
Yes a many to many relationship. And you only need to define it for one model or the other because many to many can be traversed in both direction.
Source: https://docs.djangoproject.com/en/dev/topics/db/queries/#many-to-many-relationships
Thus your models can be simplified.
class Group(models.Model):
group_name = models.CharField(max_length=50)
group_contact= models.CharField(max_length=50)
providers = models.ManyToMany(Provider, through='ProviderLocations')
class Provider(models.Model):
created_at=models.DateField(auto_now=false, auto_now_add=true)
updated_at=models.DateField(auto_now=true, auto_now_add=true)
I am not quite sure why you are trying to create both a GroupLocation
and ProviderLocation
model. I do believe they can be merged.
这篇关于在django中使用ManyToMany通过正确的复杂模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!