I'm building a cart model with the following code.
from django.db import models
class Item(models.Model):
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=8, decimal_places=2)
def __str__(self):
return self.name
class Order(models.Model):
date = models.DateTimeField(auto_now_add=True)
transcation_id = models.CharField(max_length=200, null=True)
def __str__(self):
return str(self.date)
class OrderItem(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE, blank=True, null=True)
order = models.ForeignKey(Order, on_delete=models.CASCADE, blank=True, null=True)
quantity = models.IntegerField(default=0, blank=True, null=True)
The many-to-one relationship between Item
and Order
allows one Order
to contain many Item
and that looks fine.
A model instance can be simply cloned as already answer in this question.
My problem is, if the price
of an Item
is changed. The price of contained items in Order is change too. But I don't want it to be changed. In the situation that customer already make a purchase, the price cannot be change. Is there anyway to clone the Order
instance that completely not related to the other model?