2011-07-25 35 views
0

我有這樣的功能在我的MessageAdmin:如何在Django Admin中過濾queryset以僅顯示一個條目?

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

我想回到那個有相同的組中的用戶擁有的所有消息。但是如果用戶擁有多個組,那麼所有消息都會返回兩次,所以當我嘗試打開任何消息時,出現錯誤。

編輯:UserProfile是用戶模型的擴展,我用ManyToManyField存儲所有組。

+1

我認爲你需要:Message.objects.filter(groups__in = [group_obj.id在user_profile.group.all GROUP_OBJ()])不同的() – Brandon

+0

是,這是我需要的! – Dracontis

+0

很酷。我將添加它作爲答案。如果您能將其標記爲已接受,我將不勝感激。 – Brandon

回答

1

我想你需要:

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