Reputation: 13890
I have a model for user groups:
class ProfileGroup(models.Model):
user = models.ManyToManyField(user)
name = models.CharField(max_length=100)
And a model for article:
class Article(models.Model):
title = models.CharField(max_length=200)
text = models.TextField()
group = models.ForeignKey(ProfileGroup)
owner = models.ForeignKey(user)
The user can edit own articles and articles from groups which is assigned.
How to retrieve all user articles and from his groups? Do I need to use Q? Maybe suffice filter?
articles = Article.object.filter(....
Upvotes: 0
Views: 70
Reputation: 309129
To get a user's articles:
Article.objects.filter(owner=user)
To get articles that belong to one of the user's groups.
Article.objects.filter(group__user=user)
You can then get use Q
to get articles which belong to that user or one of the user's groups.
Article.objects.filter(Q(owner=user)|Q(group__user=user))
Upvotes: 2