Discussion:
left join with and condition
(too old to reply)
Arnaldo Díaz Molina
2018-12-05 16:10:31 UTC
Permalink
Hello and thanks in advanced,

I am using Django 1.11 ORM. I need to make this SQL query:

SELECT model1.field1, sum(model2.field1), sum(model2.field2)
FROM
model1
LEFT JOIN model2 on model2.model1_id=model1.id and model2.date='2018-12-02'
group by model1.field1

I have used annotations for this kind of queries and works well when no
needed of extra conditions in left join clause.
But in this case, I need an extra condition inside left join.

How to write this using Django ORM??? Raw queries can be used, but It seems
very strange that ORM can not do this in an easy way.

Thanks again,

Arnaldo.
--
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+***@googlegroups.com.
To post to this group, send email to django-***@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/5700f3f4-de27-4ed9-99e3-85f83f064751%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mark Phillips
2018-12-05 16:43:44 UTC
Permalink
Take a look at 'Q objects' in the django documentation -
https://docs.djangoproject.com/en/2.1/ref/models/querysets/#django.db.models.Q

Perhaps F objects will help.
https://docs.djangoproject.com/en/2.1/ref/models/expressions/#django.db.models.F

Mark

On Wed, Dec 5, 2018 at 9:12 AM Arnaldo Díaz Molina <
Post by Arnaldo Díaz Molina
Hello and thanks in advanced,
SELECT model1.field1, sum(model2.field1), sum(model2.field2)
FROM
model1
LEFT JOIN model2 on model2.model1_id=model1.id and
model2.date='2018-12-02'
group by model1.field1
I have used annotations for this kind of queries and works well when no
needed of extra conditions in left join clause.
But in this case, I need an extra condition inside left join.
How to write this using Django ORM??? Raw queries can be used, but It
seems very strange that ORM can not do this in an easy way.
Thanks again,
Arnaldo.
--
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
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/5700f3f4-de27-4ed9-99e3-85f83f064751%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/5700f3f4-de27-4ed9-99e3-85f83f064751%40googlegroups.com?utm_medium=email&utm_source=footer>
.
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+***@googlegroups.com.
To post to this group, send email to django-***@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/CAEqej2OTf_%3DADZEXpZzTem29Ewenf67RymW_WM%2B1bxQX_5iytA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...