简体   繁体   中英

Select related on 3 tables in Django ORM

I need some help to perform a select_related in Django framework. My model is:

models.py

class Richiesta(models.Model):
    # TIPOLOGIE_DISPOSITIVO can be = BK or SC
    codice = models.CharField(max_length=20, null=True, blank=True, unique=True)
    ufficio_registrazione = models.ForeignKey(UfficioRegistrazione, null=True, blank=False)
    tipologia = models.CharField(max_length=50, null=False, blank=False, choices=TIPOLOGIE_DISPOSITIVO, default=TIPOLOGIE_DISPOSITIVO[0][0])
    tipo = models.IntegerField(null=False, blank=False, choices=TIPO_CHOICES, default=TIPO_PRIMO_RILASCIO)
    data_produzione = models.DateTimeField(null=True, blank=True)

class UfficioRegistrazione(models.Model):
    cliente = models.ForeignKey(Cliente, null=False, blank=False)

class Cliente(models.Model):
    # provider can be = 1 or 2
    denominazione = models.CharField(max_length=100, null=False, blank=False)
    codice_cliente = models.PositiveIntegerField(null=False, blank=False, db_index=True)
    provider = models.IntegerField(null=False, blank=False, choices=PROVIDER_CHOICES)

and there is the raw sql query I need to perform with Django ORM:

select cms_richiesta.id, cms_richiesta.tipologia, cms_richiesta.data_produzione, 
cms_richiesta.ufficio_registrazione_id, cms_ufficioregistrazione.id, 
cms_ufficioregistrazione.cliente_id, cms_cliente.id, cms_cliente.provider, 
cms_cliente.denominazione, cms_cliente.codice_cliente
from cms_richiesta INNER JOIN cms_ufficioregistrazione on 
cms_richiesta.ufficio_registrazione_id=cms_ufficioregistrazione.id
INNER JOIN cms_cliente on cms_ufficioregistrazione.cliente_id = 
cms_cliente.id where data_produzione>'2011-01-01' and data_produzione<'2017- 
12-31' and cms_cliente.provider = 2 and cms_richiesta.tipologia='BK'

can someone help me?

Richiesta.objects.filter(ufficio_registrazione__cliente__provider=2,tipologia='BK')

休息过滤器可以这样添加

You can process this as:

from datetime import datetime

Richiesta.objects.select_related(
    'ufficio_registrazione',
    'ufficio_registrazione__cliente',
).filter(
    data_produzione__range=(datetime(2011, 1, 1), datetime(2017, 12, 30)),
    ufficio_registrazione__cliente__provider=2,
    tipologia='BK',
)

Typically the .select_related(..) is here not necessary, since we perform these JOIN s already at the filtering part, but typically it is better to be explicit than implicit (especially in case you would later omit a condition, and still want these related models to be fetched).

It will retrieve Richiesta objects, but with related models already loaded.

You probably meant <= '2017-12-31' in which case the upper bound of the date range needs to be incremented as well.

我不想粘贴并复制其他答案,但是如果您认为这确实是必要的,则可以使用.values()仅提取要工作的列,因为在模板中,您将仅显示所需的数据

richiesta.values('tipologia', 'data_produzione'...) # when richiesta is the queryset

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM