Performance optimization for data connectivity in django models

In my work, I often have two tables in django models, and I need to connect them in order to return this data as csv. This data is not bound by a foreign key, but they have an identifier to connect it. This is due to the fact that we import this data from two different sources, and sometimes absent, so I can not connect it when creating a record.

My question is: what is the best way to connect this data in terms of performance, if you think that I often have to return this data?

  • The way. Create a new model that connects the data (e.g. m2m) or the parent class with the identifier to which both are connected.
class OrderInvoiceConnector(models.Model): order_data = models.ForeignKey(Order, related_name="invoice") invoice_data = models.ForeignKey(Invoice, related_name="order") 
  1. The way. Create a new model that retains only the data needed for csv export. Sort of:
 class ConnectedData(models.Model): invoice_id = models.CharField(max_length=255) country_iso = models.CharField(max_length=255) invoice_date = models.CharField(max_length=255) tax = models.FloatField(max_length=255) price = models.FloatField() 
+6
source share
1 answer

I would go with the second option, since you mentioned that the connection will be costly and data changes are made daily. If you create a read-only model, you will pack all the necessary data for users to consume in a single table query. You will need to fill in the data with some automated work, but this seems acceptable in your mentioned scenario.

+1
source

All Articles