A
A
Anton2017-10-21 16:29:03
Django
Anton, 2017-10-21 16:29:03

Django how to make a selection from the database not by fields but by the value of the model method?

There is a People model and it has a get_age method

class People(models.Model):
    last_name = models.CharField(max_length=80, blank=True)
    first_name = models.CharField(max_length=80, blank=True)
    middle_name = models.CharField(max_length=80, blank=True)
    gender = models.CharField(max_length=1)
    phone = models.CharField(blank=True, max_length=14)
    b_date = models.DateField(null=False)

        def get_age(self):
            today = date.today()
            age = today.year - self.b_date.year
            if today.month < self.b_date.month:
                age -= 1
            elif today.month == self.b_date.month and today.day < self.b_date.day:
                age -= 1
            return age

how to make a selection using the get_age method?
by the value of the fields, I know = but what option can be found by get_age? That definitely doesn't work -
People.objects.filter('first_name'='Имя')
People.objects.filter(get_age=25)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question