Dracontis
Dracontis

Reputation: 4364

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 extension for User model, where I store all groups with ManyToManyField.

Upvotes: 0

Views: 415

Answers (1)

Brandon Taylor
Brandon Taylor

Reputation: 34583

I think you need:

Message.objects.filter(groups__in = [group_obj.id for group_obj in \
    user_profile.group.all()]).distinct()

Upvotes: 1

Related Questions