How to filter a queryset based on the result of a

2019-06-08 04:49发布

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

1条回答
疯言疯语
2楼-- · 2019-06-08 05:42

You can't use python function to filter queryset. You have to "duplicate" this code and filter your objects using Q objects.

查看更多
登录 后发表回答