How can I limit a 'list_filter' to a certain subset of objects?

53 views
Skip to first unread message

Rodrigo Cea

unread,
Nov 3, 2011, 6:35:07 PM11/3/11
to django...@googlegroups.com
Explanation:

I have 3 models: Client, Project and Task. Each Client has 1 or more Projects. Each Project has 1 or more Tasks.
 
A Client can only see the Projects assigned to him and the Tasks assigned to one of his Projects. This is already working, overriding "queryset" in the ModelAdmin instance.

However, in the changelist for Tasks, there is a list_filter on Projects, so a Client can view the Tasks that are assigned to a certain Project. 

THE PROBLEM: this 'Project' filter shows all Projects, whether they're assigned to the Client or not. I need it to only show the Projects assigned to the Client.

Thanks, 

Rodrigo



Andre Terra

unread,
Nov 4, 2011, 9:56:56 AM11/4/11
to django...@googlegroups.com
Hello, Rodrigo

From looking at the ModelAdmin source code[1], I think all you need to do is reimplement the queryset() method in your subclass on admin.py.

Make sure you return a filtered queryset based on request.user. I never use the admin, so I'm not so sure about how the user will be accessible from the admin, so maybe someone else will have to explain the final steps.


Cheers,
AT

[1] https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L214


Rodrigo



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/xO-ARZBOXp4J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages