开发者

How to filter queryset in Django Admin to display only one entry?

开发者 https://www.devze.com 2023-03-22 16:02 出处:网络
I\'ve got such function in my MessageAdmin: def queryset(self, request): user_profile = UserProfile.objects.get(user = request.user.id)

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()
0

精彩评论

暂无评论...
验证码 换一张
取 消