0

I want to filter the Preschooler with their BMI tag but BMI tags are from model method. How do I filter Preschoolers with their BMI tag?

models.py

from pygrowup import Calculator, helpers

class Preschooler(Model):
    birthday = models.DateField(null=True, blank=True)
    height = models.FloatField(null=True, validators=[MinValueValidator(45.0), MaxValueValidator(120.0)])
    weight = models.FloatField(null=True, validators=[MinValueValidator(1.0), MaxValueValidator(28.0)])
    gender = models.CharField(max_length=100, choices=GENDER, null=True)

    def bmi_tag(self):
        calculator = Calculator(adjust_height_data=False, adjust_weight_scores=False,
                       include_cdc=False, logger_name='pygrowup',
                       log_level='INFO')
        
        try:
            age_months = int((date.today().year - self.birthday.year) * 12)

            whfa_value = float(calculator.wfl(self.weight, age_months, helpers.get_good_sex(str(self.gender)), self.height))
            if whfa_value > 2.0:
                return 'ABOVE NORMAL'
            elif whfa_value >= -2.0 and whfa_value <= 2.0:
                return 'NORMAL'
            elif whfa_value >= -3.0 and whfa_value < -2.0:
                return 'BELOW NORMAL'
            else:
                return 'SEVERE'
        except:
            pass

I would like to filter a preschooler like this:

preschooler_normal = Preschooler.objects.filter(bmi_tag='NORMAL')

But I am aware that we cannot query against model methods. How can I achieve this? Thanks!

Aj James
  • 73
  • 1
  • 6

0 Answers0