How to filter queryset in Django Admin to display only one entry?
I've got such function in my MessageAdmin:
def queryset(self, request):
user_profile = UserProfile.objects.get(user = request.user.id)
return Message.objects.all().filter(groups__in = [group_obj.id for group_obj in user_profile.group.all()])
I want to return all messages that have same group as User has. But with this construction all messages returns twice, if User has more than one group - so I've got error, when I try to open any message.
Edit: UserProfile is extensi开发者_如何学Goon for User model, where I store all groups with ManyToManyField.
I think you need:
Message.objects.filter(groups__in = [group_obj.id for group_obj in \
user_profile.group.all()]).distinct()
精彩评论