[英]How to filter a queryset based on the result of a method on the model class that returns a boolean?
作為我的一個模型類的成員函數,我有一個is_visible(self, user)
方法,該方法返回一個布爾值。 按照定義,它將請求用戶(Django User
模型)作為輸入。
我希望能夠基於對此方法的響應來過濾查詢集。 如何使用此功能作為查詢集過濾器?
對於上下文,這是我的is_visible
實現:
def is_visible(self, user):
if self.status.status_internal == "open":
return True
if self.owner == user:
return true
participations = Participation.objects.filter(event__id=self.id, participant__id=user.id)
if len(participations) > 0:
return True
if self.status.status_internal == "invite":
return True
return False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.