Codejoy Codejoy - 5 months ago 56
Python Question

Using ManyToMany through correctly for complex model in django

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.

Answer

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.