Всем привет!
Для получения queryset используется сложный запрос к БД. Как его переписать, используя джанговские встроенные критерии я не знаю. Выполняя raw запрос я получаю RawQuerySet, но мне нужен QuerySet в итоге. Может кто-то подскажет как можно сделать? Или как прееписать этот запрос, используя filter, exclude, annotations и т.п., используя джанговский функционал?
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'