A
A
Antigo_ptz2017-03-24 11:16:30
Django
Antigo_ptz, 2017-03-24 11:16:30

Django QuerySet and raw sql query?

Hello!
To get the queryset, a complex database query is used. I don't know how to rewrite it using Jung's built-in criteria. By executing a raw query, I get a RawQuerySet, but I need a QuerySet in the end. Can someone suggest how to do it? Or how to prewrite this query using filter, exclude, annotations, etc. using jung functionality?

queryset = Patient.objects.raw(
'select * from mpi_patient join mpi_address on mpi_address.patient_id = mpi_patient.id and fias in (select aoguid from nsi_fias where id = %s)',[fias_id])

class Patient(CommonInfo):
    id = models.BigAutoField(primary_key=True)
    last_name = models.CharField(max_length=125, db_index=True)
    first_name = models.CharField(max_length=125, db_index=True)
    second_name = models.CharField(max_length=125, blank=True, db_index=True)

    class Meta:
        db_table = "mpi_patient"


class Address(CommonInfo):
    id = models.BigAutoField(primary_key=True)
    patient = models.ForeignKey(Patient, related_name='addresses') 
    address_type = models.ForeignKey(DirectoryItem, models.PROTECT, related_name='+') 
    fias = models.CharField(max_length=125, blank=True)

    class Meta:
        db_table = "mpi_address"


class Fias(models.Model):
    id = models.BigAutoField(primary_key=True)
    aoid = models.CharField(max_length=125, null=True, blank=True)
    aoguid = models.CharField(max_length=125, db_index=True, null=True, blank=True)
    formalname = models.CharField(max_length=125, null=True, blank=True)
    regioncode = models.CharField(max_length=125, null=True, blank=True)
    autocode = models.CharField(max_length=125, null=True, blank=True)
    areacode = models.CharField(max_length=125, null=True, blank=True)
    citycode = models.CharField(max_length=125, null=True, blank=True)
    ctarcode = models.CharField(max_length=125, null=True, blank=True)
    placecode = models.CharField(max_length=125, null=True, blank=True)
    plancode = models.CharField(max_length=125, null=True, blank=True)
    streetcode = models.CharField(max_length=125, null=True, blank=True)
    extrcode = models.CharField(max_length=125, null=True, blank=True)
    sextcode = models.CharField(max_length=125, null=True, blank=True)
    offname = models.CharField(max_length=125,null=True, blank=True)
    postalcode = models.CharField(max_length=125,null=True, blank=True)
    ifnsfl = models.CharField(max_length=125, null=True, blank=True)
    terrifnsfl = models.CharField(max_length=125, null=True, blank=True)
    ifnsul = models.CharField(max_length=125, null=True, blank=True)
    terrifnsul = models.CharField(max_length=125, null=True, blank=True)
    okato = models.CharField(max_length=125, null=True, blank=True)
    oktmo = models.CharField(max_length=125, null=True, blank=True)
    updatedate = models.DateField(null=True, blank=True)
    shortname = models.CharField(max_length=125, null=True, blank=True)
    aolevel = models.SmallIntegerField(db_index=True, null=True, blank=True)
    parentguid = models.CharField(db_index=True, max_length=125,null=True, blank=True)
    previd = models.CharField(max_length=125, null=True, blank=True)
    nextid = models.CharField(max_length=125, null=True, blank=True)
    code = models.CharField(max_length=125, null=True, blank=True)
    plaincode = models.CharField(max_length=125, null=True, blank=True)
    actstatus = models.SmallIntegerField(null=True, blank=True)
    livestatus = models.SmallIntegerField(db_index=True, null=True, blank=True)
    centstatus = models.CharField(max_length=125, null=True, blank=True)
    operstatus = models.CharField(max_length=125, null=True, blank=True)
    currstatus = models.CharField(max_length=125, null=True, blank=True)
    startdate = models.DateField(null=True, blank=True)
    enddate = models.DateField(null=True, blank=True)
    normdoc = models.CharField(max_length=125, null=True, blank=True)
    cadnum = models.CharField(max_length=125, null=True, blank=True)
    divtype = models.SmallIntegerField(null=True, blank=True)


    class Meta:
        db_table = 'nsi_fias'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
un1t, 2017-03-24
@un1t

For such requests https://github.com/ambitioninc/django-query-builder can help

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question