[Fixed]-Django Copy Related Data and keep them unchanged over time

1👍

This is a pretty broad problem with multiple solutions. I dont think that what you’re aiming to is the correct one.

One rule for saving invoices is, that invoices never change. You should never update an invoice. So not only your ‘copies’ of invoices should remain the same, but the original too.

Also, you should have a InvoiceItem (or InvoiceRow) model which are the items on your invoice. Don’t bind Products to a Invoice directly.

Here are 2 solutions I’ve used:

Solution 1

You can normalize the data on your invoice(items). So, don’t use foreignkeys, but normalize all data about the product, so product info (incl. price) is saved within the invoice(item).

Solution 2

Give your products revision numbers. So everytime a product is updated (name or price change for example), a new product is created in the database. Now you can link the InvoiceItem to a Product with a Foreign Key, and it will will be historically accurate.

Im sure there are some guides/best practices for creating Invoice backends. Language or Framework is not important. Invoicing is really important, so do alot of research before starting to build something. That’s just my two pennies

Leave a comment