Django: Complex filter parameters or...?
Posted
by minder
on Stack Overflow
See other posts from Stack Overflow
or by minder
Published on 2010-04-12T09:17:26Z
Indexed on
2010/04/12
9:23 UTC
Read the original article
Hit count: 568
This question is connected to my other question but I changed the logic a bit.
I have models like this:
from django.contrib.auth.models import Group
class Category(models.Model):
(...)
editors = ForeignKey(Group)
class Entry(models.Model):
(...)
category = ForeignKey(Category)
Now let's say User logs into admin panel and wants to change an Entry. How do I limit the list of Entries only to those, he has the right to edit? I mean: How can I list only those Entries which are assigned to a Category that in its "editors" field has one of the groups the User belongs to?
What if User belongs to several groups? I still need to show all relevant Entries.
I tried experimenting with changelist_view() and queryset() methods but this problem is a bit too complex for me.
I'm also wondering if granular-permissions could help me with the task, but for now I have no clue.
I came up only with this: First I get the list of all Groups the User belongs to. Then for each Group I get all connected Categories and then for each Category I get all Entries that belong to these Categories. Unfortunately I have no idea how to stitch everything together as filter() parameters to produce a nice single QuerySet.
© Stack Overflow or respective owner