I need to build a class structure that allows me to create multiple PackItem
s for a single Item
in the admin panel. Each PackItem
should have a quantity
field and a reference to the Item
it relates to.
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.DecimalField(decimal_places=2, max_digits=10)
is_pack = models.BooleanField(default=False)
pack_items = models.ManyToManyField('PackItem', blank=True)
class PackItem(models.Model):
packitem = models.ForeignKey('Item', on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
I’m currently creating PackItem
s one by one, and then adding them to the pack_items
field of the Item
model. Is this the best way to do it, or is there a better practice?