Re: Query with 3 models

33 views
Skip to first unread message

Tomas Neme

unread,
Jul 19, 2012, 10:52:11 AM7/19/12
to django...@googlegroups.com

> {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.

Python_Junkie

unread,
Jul 19, 2012, 1:30:54 PM7/19/12
to django...@googlegroups.com
If you are comfortable with sql syntax, just pull the data you want with sql joins.

You already have the keys set up

On Thursday, July 19, 2012 5:16:42 AM UTC-4, Julio Galvez wrote:
Hi, I'm new with Django and in this Groups; I have the next models and question

class Empresa(models.Model):
usuario = models.ForeignKey(User)
empresa = models.CharField(max_length=100, unique=True)
slogan =  models.TextField()
logotipo = models.ImageField(upload_to='logos')
descripcion = models.TextField()
compra_minima = models.FloatField(verbose_name='Compra Mínima')
costo_envio = models.FloatField(verbose_name='Costo de Envío')

def __unicode__(self):
return self.empresa

class Sucursal(models.Model):
empresa = models.ForeignKey(Empresa)
direccion = models.CharField(max_length=200, null=False)
estado = models.CharField(max_length=30)
municipio = models.CharField(max_length=30)

def __unicode__(self):
return self.direccion

class Platillo(models.Model):
empresa = models.ForeignKey(Empresa)
categoria = models.ForeignKey(Categoria_platillo)
nombre_platillo = models.CharField(max_length=100, null=False, blank=False, verbose_name='Nombre del Platillo')
fotografia = models.ImageField(upload_to='platillos', verbose_name='Fotografía')
descripcion = models.TextField()
precio = models.FloatField()
dia_existencia = models.CharField(max_length=150)
fecha_publicacion = models.DateTimeField(auto_now=True)
tags = models.CharField(max_length=100)
estatus = models.BooleanField(null=False, default=True)

def __unicode__(self):
return self.nombre_platillo

class Horario(models.Model):
empresa = models.ForeignKey(Empresa)
dia = models.CharField(max_length=15)
hora_abre = models.CharField(max_length=10)
hora_cierra = models.CharField(max_length=10)

def __unicode__(self):
return self.hora_abre


Now, I want to create a result like this:

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

I use "select_related()" method, but I can't do it

Thomas Orozco

unread,
Jul 19, 2012, 1:38:04 PM7/19/12
to django...@googlegroups.com

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

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

Julio Galvez

unread,
Jul 19, 2012, 4:01:24 PM7/19/12
to django...@googlegroups.com
Thanks, the first example is exactly that i want. I try it in the Django Shell and it works, but; How I show the results in the template?

Maybe I'm wrong, but I try to pass like  { 'empresas': Empresa.objects.all().select_related() } ,and only can show the 'Empresa' attributes on the loop.

I  do something like this

{% for a in empresa%}
  a.slogan
  a.descripcion
  a.platillo.fotografia
{% endfor %}

Tomas Neme

unread,
Jul 19, 2012, 4:07:44 PM7/19/12
to django...@googlegroups.com
as I said, you need to do a loop like the one I showed in python, but
in the template

say, for example

{% for empresa in empresas %} {# keeping your plurals straight is good
practice! #}
{% for sucursal in empresa.sucursal_set.all %}
{% for platillo in empresa.platillo_set.all %}
{% for horario in empresa.horario_set.all %}
<p>
empresa.slogan<br />
empresa.descripcion<br />
platillo.fotografia<br />
horario.foobar<br />
</p>
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}

Julio Galvez

unread,
Jul 19, 2012, 4:34:17 PM7/19/12
to django...@googlegroups.com
That works awesome!!. I'm new in this, and I don't know many things about how works django. 

Thanks all for help me
Reply all
Reply to author
Forward
0 new messages