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")
- 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()
source share