Assaf Lavie
Assaf Lavie

Reputation: 75973

Filter models by ManyToMany relationship with Django User

Given this model:

from django.db import models
from django.contrib.auth.admin import User

# Create your models here.
class Plan(models.Model):
    editors = models.ManyToManyField(User)

in which every plan can have more than one editor (User), how can I retrieve all plans for which a particular user is one of the editors?

Something like this?

Plan.objects.filter(editors__contains(request.user))

?

Upvotes: 1

Views: 506

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599580

You just use the normal equals here.

Plan.objects.filter(editors=request.user)

Upvotes: 2

Related Questions