Thursday, July 19, 2012

Re: Query with 3 models

Shouldn't that be a prefetch related and not select related?

Le 19 juil. 2012 16:52, "Tomas Neme" <lacrymology@gmail.com> a écrit :

> {Empresa1, Sucursal1, Platillo1, Horario1},
> {Empresa1, Sucursal1, Platillo2, Horario1},
> {Empresa2, Sucursal1, Platillo1, Horario1}...

I'm guessing the Sucursal1 in the first and third lines are not the same? I mean, each Sucursal points to only one Empresa, so I'm guessing you want only the related objects

if this is the case, you can do something like this:

[(empresa, sucursal, platillo, horario) for empresa in Empresa.objects.all().select_related()
                                        for sucursal in empresa.sucursal_set.all()
                                        for platillo in empresa.platillo_set.all()
                                        for horario in empresa.horario_set.all()]


that if what you want is a list. If you want just to loop through those, you can do

for empresa in Empresa.objects.all().select_related():
    for sucursal in empresa.sucursal_set.all():
        for platillo in empresa.platillo_set.all():
            for horario in empresa.horario_set.all():
                do_something(empresa, sucursal, platillo, horario)

and about the same thing if you want to cycle through them in a template, in which case you can just pass { 'empresas': Empresa.objects.all().select_related() } and then loop like above on the template side

--
"The whole of Japan is pure invention. There is no such country, there are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment