I am working on a Django project and stuck at a problem. My models look like this:
class Products(models.Model):
basket_name = models.CharField(max_length = 5, blank = False)
product_name = models.CharField(max_length = 30, blank = False)
quantity = models.PositiveSmallIntegerField(null = False, blank = False, default=1)
class ShelfProduct(models.Model):
shelf_name = models.CharField(max_length = 15, default="defaultshelf")
products = models.ManytoManyField(Products)
..... other fields....
class KioskShelf(models.Model):
kiosk_name = models.CharField(max_length= 15, default="default")
shelfproduct = models.ManytoManyField(ShelfProduct)
...other fields....
class MapperModel(models.Model):
kioskshelf = models.ForeignKey(KioskShelf, on_delete = models.CASCADE)
...other fields....
I am trying to update the product quantity in Products models for a particular kiosk name and particular shelf. I tried like this:
data = MapperModel.objects.filter(kioskshelf__kiosk_name = 'kiosk1').filter(kioskshelf__shelfproduct__shelf_name = 'Shelf A')
But after this am not sure how to update the quantity in Products table. Am not even so sure if my approach is correct. Please assist me how to do it. Thanks a lot in advance.
You need to go through the Products
table. Have you tried:
Products.objects.filter(shelfproduct__shelf_name='Shelf A', shelfproduct__kioskshelf__kiosk_name='kiosk1').update(quantity=<quantity>)