django - Displaying only children of objects created by the user in Admin -
there following relation in django models:
django user
--has many-- competition
--has many-- category
--has many-- competitor
in admin want show objects created logged in user
. can competition
- no problem: in admin.py
override get_queryset
of model admin , assign model:
class competitionadmin(admin.modeladmin): def get_queryset(self, request): qs = super(competitionadmin, self).get_queryset(request) if request.user.is_superuser: return qs return qs.filter(user_id=request.user.id) admin.site.register(competition,competitionadmin)
how can same thing category
, consequently competitor
?
you can reference foreign key relation double underscore notation:
for category
if category
has foreignkey field competition
:
category.objects.filter(competition__user=request.user)
for competitor
if competitor
has foreignkey field category
:
competitor.objects.filter(category__competition__user=request.user)
Comments
Post a Comment