Maybe it's just me, but I had a report I was generating that took 3.5 minutes to complete that utilized for loops and the ORM. I managed to generate the same report in 18 seconds using a RawSQL query. So what's the upside? Speed. I would understand if the times were comparable, but they're not. I find myself becoming more and more blocked by the limited functionality of the ORM in this regard. Perhaps it's just my application--maybe Django isn't the best fit for it.
But if in your use case, you had a million records in which you were trying to filter by age using Python loops, I have a suspicion that it would be slower than using the database functions that Django does include. (Saying that, I would like to see the Django community make a greater attempt to making those functions more pythonic.) -----Original Message----- From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Melvyn Sopacua Sent: Monday, May 14, 2018 9:29 AM To: django-users@googlegroups.com Subject: Re: SQL select statements to Django ORM On maandag 14 mei 2018 15:38:01 CEST Matthew Pava wrote: > 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/#c > ast > https://docs.djangoproject.com/en/2.0/ref/models/database-functions/#n > ow Sure, but what's the upside? The downside is that your objects are bigger, more data transferred from db to python layer, more complex query. What I'm trying to conveigh here, is not to shoehorn the ORM into your notion of SQL, but to think in models, model fields, object properties and model relations. There will be times where the ORM needs help from SQL, but pick your battles. -- 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/4047248.Nn77cOrblS%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/a1fcdf93225e48f4a23ea42fff240cac%40ISS1.ISS.LOCAL. For more options, visit https://groups.google.com/d/optout.