You could use the ORM to get the difference of two dates like so: Person.objects.annotate(age=ExpressionWrapper(Cast(Now(), DateField()) - F('dob'), output_field=DurationField())) https://docs.djangoproject.com/en/2.0/ref/models/database-functions/#cast https://docs.djangoproject.com/en/2.0/ref/models/database-functions/#now
-----Original Message----- From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Melvyn Sopacua Sent: Monday, May 14, 2018 5:56 AM To: django-users@googlegroups.com Subject: Re: SQL select statements to Django ORM On zondag 13 mei 2018 03:44:37 CEST Gerald Brown wrote: > As I have said previously, I am new to Django ORM so I would like to > know howto/find reference to Django ORM Query to the following SQL > Select > statements: Do you want to use SQL or use Django? Pick one. > 1. From Mariadb. > > SELECT name, date_of_birth, > *TIMESTAMPDIFF(YEAR,date_of_birth,'2014-08-02'/or curdate())* AS age > FROM some table. The Django way: Use case: I want to gather a person's name, date of birth and age for all people modeled using Person. from datetime import date class Person(models.Model): name = models.CharField(max_length=100) dob = models.DateField() @property def age(self) -> int: diff = date.today() - self.dob return diff.year people = Person.objects.defer('name', 'dob').all() > 2. General SQL statement > SELECT field1, field2, field10 from some table See defer() as above. > 3. How to see all of the data from those statements which SQL shows me. Use case: I want to show people's name, date of birth and age. Using above "people": print('name', 'date of birth', 'age') for person in people: print(people.name, people.dob, people.age) -- Melvyn Sopacua -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4418595.HAJx60YL8B%40fritzbook. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8f9fca34d3464ace8a0720cd374b15b7%40ISS1.ISS.LOCAL. For more options, visit https://groups.google.com/d/optout.