Reputation: 6254
I have a django model like this:
class Tour(models.Model):
Name=models.CharField(max_length=100)
Count=models.SmallIntegerField()
PriceUnitCode=models.ForeignKey(PriceUnit)
Price=models.CharField(max_length=12)
Description=models.TextField()
ActionDate=models.CharField(max_length=16)
ActionUser=models.ForeignKey(User)
When a user logs in the admin site, I would want the user to see only the Tour instances he has created (Tour instances for which the ActionUser is equal to the ID of logged-in user).
How can I filter the changelist based on logged-in users?
Upvotes: 1
Views: 1344
Reputation: 6254
Thanks buddy I tryed your code but it had error!!so I changed it and now it work well:
class TourAdmin(admin.ModelAdmin):
def queryset(self, request):
qs = super(TourAdmin, self).queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(ActionUserCode=request.user)
admin.site.register(Tour,TourAdmin)
I put code for others who may have such problem
Upvotes: 4
Reputation: 3394
in admins.py
class MyRegisteredTourAdmin(admin.ModelAdmin):
def queryset(self, request):
qs = Tour.objects.filter(action_user=request.user)
return qs
admin.site.register(Tour,MyRegisteredTourAdmin)
Upvotes: 1