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