在django中使用ManyToMany通过正确的复杂模型 [英] Using ManyToMany through correctly for complex model in django

查看:107
本文介绍了在django中使用ManyToMany通过正确的复杂模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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关系系统,所以对如何使这些关系能很好地协同工作的任何建设性的批评将是有帮助的。

解决方案


我的问题是,我的Group(和/或Provider)模型是否需要
在模型定义中指定的某种关系?




是一对多关系。而您只需要为一个模型或另一个模型定义它,因为可以在两个方向上遍历多个对象。


两对多关系的两端都可以自动访问
。 API的作用就像上面的向后一对多
关系。



唯一的区别是在属性命名中:定义的模型
ManyToManyField使用该字段本身的属性名称
,而反向模型使用
原始模型的较低的模型名称,加上_set(就像反向一到-many
关系)。


资料来源: https://docs.djangoproject.com/en/dev/topics/db/queries/#many-to-many -relationships



因此,您的模型可以被简化。

  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 locations So

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 locations and 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.

解决方案

My question is, does my Group (and/or Provider) model need to have some sort of relationship specified in their model definitions?

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.

Both ends of a many-to-many relationship get automatic API access to the other end. The API works just as a "backward" one-to-many relationship, above.

The only difference is in the attribute naming: The model that defines the ManyToManyField uses the attribute name of that field itself, whereas the "reverse" model uses the lowercased model name of the original model, plus '_set' (just like reverse one-to-many relationships).

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通过正确的复杂模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆