私はショッピングカートを構築しています。多くのオブジェクトを1つの書式で更新する
class Product(models.Model):
name = models.CharField(max_length=64)
description = models.TextField(blank=True)
unit = models.CharField(max_length=16)
price_per_unit = models.DecimalField(max_digits=11, decimal_places=2)
image = StdImageField(default="default.jpg", variations={
'thumbnail': {"width": 100, "height": 100, "crop": True}
})
def __str__(self):
return self.name
class ShoppingCartItem(models.Model):
user = models.ForeignKey(django_settings.AUTH_USER_MODEL, related_name='cart_items', on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=1)
# Update the quantity of an item in the shopping cart
def update_quantity(self, quantity):
if quantity <= 0:
# If user sets quantity to 0 or less, delete the item.
self.remove_item()
else:
self.quantity = quantity
self.save()
# Remove item from the shopping cart
def remove_item(self):
self.delete()
def get_ext_total(self):
return Decimal(self.product.price_per_unit * self.quantity)
私はUXは一度に多くのShoppingCartItem
のためのテキストボックスを経由して量を更新することができるようにしたい:私はProduct
モデルとShoppingCartItem
モデルを持っています。私はフォームセットとインラインフォームセットを見てきましたが、一度に多くのオブジェクトを更新するのではなく、オブジェクトの作成や各オブジェクト内のManyToMany関係に適しているようです。以下は私のショッピングカートの例である:
これを行うことについて移動する最良の方法は何ですか?