I'm trying to get all loyal clients in my Django app. For this, I'm trying to filter a queryset using a method, rather than an attribute, and I don't know how to do this.
EDIT : using Kedar advice, I tried to to this with @property
decorator
class Client(models.Model):
@property
def is_loyal(self):
# ...
# Return True or False
@classmethod
def get_loyal_clients_since(cls):
# Method #1: works but clearly a workaround
base_clients = cls.some_method_to_retrieve_special_clients()
clients_list = [c.pk for c in base_clients if c.is_loyal()]
clients = cls.objects.filter(pk__in=clients_list)
# Method #2: does not work
clients = cls.some_method_to_retrieve_special_clients() \
.filter(is_loyal=True)
Error: Cannot resolve keyword 'is_loyal' into field.