Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the 'reverse' of a Django ManyToMany relationship?

I'm coming from a Rails background, and am having a bit of trouble making use of the "Association Methods" provided in Django. I have two models (which have been simplified for the sake of brevity), like so:

class User(models.Model):     username = models.CharField(max_length=100, unique=True)     companies = models.ManyToManyField('Company', blank=True)  class Company(models.Model):     name = models.CharField(max_length=255) 

According to the Django documentation:

"It doesn't matter which model has the ManyToManyField, but you should only put it in one of the models -- not both.".

So I understand that if I have an instance of a User, called user, I can do:

user.companies 

My question is how do I do the reverse? How do I get all users that belong to a Company instance, let's say Company:

company.users # This doesn't work! 

What's the convention to do this? The documentation that I've read doesn't really cover this. I need the association to work both ways, so I can't simply move it from one model to the other.

like image 861
Mike Trpcic Avatar asked Feb 19 '12 19:02

Mike Trpcic


People also ask

How do I reverse a relation in Django?

Thats' where related name or the reverse relationship comes in. Django, by defaults gives you a default related_name which is the ModelName (in lowercase) followed by _set - In this case, It would be profile_set , so group. profile_set . However, you can override it by specifying a related_name in the ForeignKey field.

How do you do a many-to-many relationship in Django?

To define a many-to-many relationship, use ManyToManyField . What follows are examples of operations that can be performed using the Python API facilities. You can't associate it with a Publication until it's been saved: >>> a1.

How does many-to-many field work in Django?

The invisible "through" model that Django uses to make many-to-many relationships work requires the primary keys for the source model and the target model. A primary key doesn't exist until a model instance is saved, so that's why both instances have to exist before they can be related.

How do you save a many-to-many field in Django?

If your model has a many-to-many relation and you specify commit=False when you save a form, Django cannot immediately save the form data for the many-to-many relation. This is because it isn't possible to save many-to-many data for an instance until the instance exists in the database.


1 Answers

company.user_set.all() 

will return a QuerySet of User objects that belong to a particular company. By default you use modelname_set to reverse the relationship, but you can override this be providing a related_name as a parameter when defining the model, i.e.

class User(models.Model):     companies = models.ManyToManyField(Company, ..., related_name="users")  > company.users.all() 

here is the relevant documentation

like image 177
Timmy O'Mahony Avatar answered Nov 07 '22 10:11

Timmy O'Mahony