views:

227

answers:

1

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.

EDIT:

I tried to use MultiQuerySet like this:

class EntryAdmin(admin.ModelAdmin):

    (...)

    def queryset(self, request):
        qs = super(EntryAdmin, self).queryset(request)
        if not request.user.is_superuser:
            groups = request.user.groups.all()
            list = []
            for group in groups:
                categories = Category.objects.filter(editors=group)
                for category in categories:
                    results = qs.filter(category=category)
                    list.append(results)
            qs = MultiQuerySet(*list)
        return qs

but I get a database error in the admin view (NOT a Django error; normal admin page is displayed but instead of a list there's "Database error")

+1  A: 

You can try using the __in operator:

qs = Category.objects.filter(editors__in=request.user.groups.all())
Ofri Raviv
Ohmigod, that did the trick! I used this: qs = qs.filter(category__in=Category.objects.filter(editors__in=request.user.groups.all())) THANKS!
minder