pablo
pablo

Reputation: 2809

Model objects versions in Django

I'm building an e-commerce website. I have a Product and Order models. It's possible that a customer order a product and then the admin change its price or other fields before the customer actually get it.

A possible solution is to add a 'version' field to the Product model. When the admin update a product field I'll add a timestamp and create a new object instead of updating the old one. An Order will have a reference to a specific product version.

Does this make sense? Will overriding the Product Save method be sufficient to make it work?

Thanks

Upvotes: 0

Views: 841

Answers (1)

Seth
Seth

Reputation: 46443

I do it this way:

Make a duplicate of the relevant product columns in the order table.

When you add a product to the order, copy everything from Product to Order (include a FK too if you want). That way the admin can do whatever they want (change product name/price/category/etc.), but the product price / name / etc. will always remain the same.

You could do a version column in product as you suggested, but that's a lot more complicated.

Upvotes: 1

Related Questions