Consult several Django models


I have these models:

class TablaA(models.Model):
    estado = models.BooleanField()
    # mas campos...

class TablaB(models.Model):
    tabla_a = models.ForeignKey(TablaA)
    estado = models.BooleanField()
    # mas campos...

class TablaC(models.Model):
    tabla_a = models.ForeignKey(TablaA)
    # mas campos...

What I need is to have the data of TablaC when the fields estado of TablaA and% TablaB are False .

How is it possible to do this?

asked by Christian M 17.01.2017 в 01:14

2 answers


ps you could do it like that ..

from yourapp import models
a = models.TablaA
b = models.TablaB
c = models.TablaC
if a.estado == False and b.estado == False:
  return c.tabla_a

You can return c.table_a or any other data that is in the table.

You tell me how you are doing


answered by 17.01.2017 в 02:47

I solved it using select_related() and FOO_set .

data = TablaB.objects.filter(tabla_a__Estado=False)

And in the template when going through:

{% for d in data %}
    {{ }}
{% endfor %}

Taking into account that in TablaB there is also a reference to TablaC.

However now, in another section of the application, I need to do it in a single query so that in the template I can only go through and no longer make an additional query. I still can not do it.

answered by 17.01.2017 в 06:51