V
V
Vitaly Ananiev2022-04-06 18:09:12
Django
Vitaly Ananiev, 2022-04-06 18:09:12

How do I call a function in a filter?

Let's say I have a model in models

class User(AbstractUser):
    last_online = models.DateTimeField(verbose_name='Online', blank=True, null=True)

    def is_online(self):
        if self.last_online:
            return (timezone.now() - self.last_online) < timezone.timedelta(minutes=15)
        return False

    def get_online_info(self):
        if self.is_online():
            return translator.translate(_('Online'))
        if self.last_online:
            return translator.translate(_('Last visit {}').format(naturalday(self.last_online)))

        return _('Unknow')

    def get_online(self):
        if self.is_online():
            return True
        else:
            return False

And I want to make filtering in views. Use the get_online function in the filter. Example:
online = User.objects.filter(last_online__get_online=) 'last_online__get_online' - the code is not written correctly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2022-04-06
@Vitalii181190

in any way, you do not distinguish call levels, in application or sql request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question