Skip to content

Commit

Permalink
refactor: using prefetch_related to optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
TanookiVerde committed Jan 4, 2024
1 parent e77773a commit 7d40ae5
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions api/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

class Address(Model):
id = fields.UUIDField(pk=True)
use = fields.ForeignKeyField("app.AddressUse", related_name="addresses", null=True)
type = fields.ForeignKeyField("app.AddressType", related_name="addresses", null=True)
use = fields.ForeignKeyField("app.AddressUse", related_name="use", null=True)
type = fields.ForeignKeyField("app.AddressType", related_name="type", null=True)
line = fields.CharField(max_length=1024)
city = fields.ForeignKeyField("app.City", related_name="addresses")
city = fields.ForeignKeyField("app.City", related_name="city")
# state: contained in city.
postal_code = fields.CharField(max_length=8, null=True)

Expand Down Expand Up @@ -309,13 +309,7 @@ async def to_pydantic_model(self) -> PatientModel:
try:
address_patient_periods_instances = await self.address_patient_periods.all()
for address_patient_period in address_patient_periods_instances:
# TODO: Refactor this part
address = await address_patient_period.address
type = await address.type
use = await address.use
city = await address.city
state = await city.state
country = await state.country
address = await address_patient_period.address.prefetch_related('use','type','city', 'city__state', 'city__state__country')

if address_patient_period.period_start:
period = PeriodModel(
Expand All @@ -327,12 +321,12 @@ async def to_pydantic_model(self) -> PatientModel:

addresses.append(
AddressModel(
use=use.name if use else None,
type=type.name if type else None,
use=address.use.name if address.use else None,
type=address.type.name if address.type else None,
line=address.line,
city=city.name,
state=state.name,
country=country.name,
city=address.city.name,
state=address.city.state.name,
country=address.city.state.country.name,
postal_code=address.postal_code,
period=period,
)
Expand All @@ -344,9 +338,7 @@ async def to_pydantic_model(self) -> PatientModel:
try:
telecom_patient_period_instances = await self.telecom_patient_periods.all()
for telecom_patient_period in telecom_patient_period_instances:
telecom = await telecom_patient_period.telecom
use = await telecom.use
system = await telecom.system
telecom = await telecom_patient_period.telecom.prefetch_related('use','system')

if telecom_patient_period.period_start:
period = PeriodModel(
Expand All @@ -357,8 +349,8 @@ async def to_pydantic_model(self) -> PatientModel:
period = None
telecoms.append(
TelecomModel(
system=system.name if system else None,
use=use.name if use else None,
system=telecom.system.name if telecom.system else None,
use=telecom.use.name if telecom.use else None,
value=telecom.value,
rank=telecom.rank,
period=period,
Expand Down Expand Up @@ -411,8 +403,8 @@ class State(Model):

class Telecom(Model):
id = fields.UUIDField(pk=True)
system = fields.ForeignKeyField("app.TelecomSystem", related_name="telecoms", null=True)
use = fields.ForeignKeyField("app.TelecomUse", related_name="telecoms", null=True)
system = fields.ForeignKeyField("app.TelecomSystem", related_name="system", null=True)
use = fields.ForeignKeyField("app.TelecomUse", related_name="use", null=True)
value = fields.CharField(max_length=512)
rank = fields.IntField(null=True)

Expand Down

0 comments on commit 7d40ae5

Please sign in to comment.