Let's say, we have two models:
class User(models.Model):
nickname = models.CharField(max_length=50)
email = models.CharField(max_length=50)
class Profile(models.Model):
user = models.OneToOneField(User, primary_key=True)
carma = models.BooleanField(default=False)
birthdate = models.DateField(input_formats=['%Y-%m-%d'])
How to get amount of users where:
1) all users with ages below 18?
2) all users between 18 - 25
3) all users above 25
As I understood, I need to get an age from birthdate, e.g. this way:
def age(self):
import datetime
return int((datetime.date.today() - self.birthday).days / 365.25 )
and use returned age in query somehow.
How to perform these cross-requests with function inside (for each specified query) in Django?
Thank you!
Based on this answer.