As a member function of one of my model classes, I have an is_visible(self, user)
method that returns a boolean. As defined, it takes the requesting user (Django User
model) as input.
I would like to be able to filter querysets based on the response to this method. How can I use this function as a queryset filter?
For context, here is my is_visible
implementation:
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
You can't use python function to filter queryset. You have to "duplicate" this code and filter your objects using Q objects.