Reputation: 581
I am working on basic ecommerce website in django.I am having the following view that checks for a successful order.
def handlerequest(request, id):
order=Order.objects.get(id=id)
items=order.orderitem_set.all()
verify = Checksum.verify_checksum(response_dict, MERCHANT_KEY, checksum)
if verify:
if response_dict['RESPCODE'] == '01':
order.save()
print('order successful')
else:
order.dstatus="Cancelled"
order.save()
return render(request, 'handlerequest.html', {'response': response_dict,'order':order})
The items here refer to the products that a user has chosen to buy.My product models is as below with size model.(I have removed some fields)
class Siz(models.Model):
size=models.CharField(max_length=3,null=True,blank=True)
class Product(models.Model):
name=models.CharField(max_length=10)
size=models.ManyToManyField(Siz)
What I have done is that in my siz model I have added several sizes(eg. S,L,M) and for product I have selected the sizes which are available for each product. Now my concern is that whenever someone orders something then after checking in the view that the order is successful , I want to remove the size of the item ordered from the product.(For Example if someone orders a product AB of size L then from available size of that product I want to remove L) The following is my orderitem and order model for reference.
class Order(models.Model):
customer=models.ForeignKey(Customer,on_delete=models.SET_NULL,null=True,blank=True)
complete=models.BooleanField(default=False,null=True,blank=False)
class OrderItem(models.Model):
product=models.ForeignKey(Product,on_delete=models.SET_NULL,null=True)
order=models.ForeignKey(Order,on_delete=models.SET_NULL,null=True,)
size=models.ForeignKey(Siz,on_delete=models.SET_NULL,null=True)
I also want to keep in mind that if there are multiple items that a user orders then the sizes that are ordered should be removed for all products . I tried working around it but nothing worked for me. So any help would be appreciated.
Upvotes: 0
Views: 170
Reputation: 3076
Considering OrderItem
has a specific Size
associated with it (the ForeignKey), I assume that a single order cannot contain multiple sizes (so, it can have multiple products of same size, but not multiple products of various size). If that's the case, I suggest changing the model a bit so that you keep track of how many Products you've got available:
class Size(models.Model):
size=models.CharField(max_length=3,null=True,blank=True)
class Product(models.Model):
name=models.CharField(max_length=10)
class ProductItem(models.Model): # the "warehouse", ie how many we've got available
product=models.ForeignKey(Product)
size=models.ForeignKey(Size)
quantity=models.IntegerField() # how many still available
class Order(models.Model):
customer=models.ForeignKey(Customer,on_delete=models.SET_NULL,null=True,blank=True)
complete=models.BooleanField(default=False,null=True,blank=False)
class OrderItem(models.Model):
product=models.ForeignKey(Product,on_delete=models.SET_NULL,null=True)
order=models.ForeignKey(Order,on_delete=models.SET_NULL,null=True,)
size=models.ForeignKey(Size,on_delete=models.SET_NULL,null=True)
quantity=models.IntegerField() # how many within a single
Notice the quantity
property, both within the OrderItem
(how many ordered) and ProductItem
(how many still available). This way you could easily keep track of how many products you got left.
I think that such many-to-many relationship isn't really correct in this context, as the Size
table is some sort of enumeration - you'll add, let's say, 10 various sizes there and should never touch that table again, right? Great example of many-to-many relationship is a Book
and Author
, because there'll be multiple Books by multiple Authors and none of this objects is, in general, an "enumeration" or something with finite set of "possible values". In the end, Size
itself does not really belong to any Product
, right?
Upvotes: 1