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 [email protected].
To post to this group, send email to [email protected].
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.

Reply via email to