[Answered ]-Is there any way to store repeating foreign keys in M2M field in Django?

1👍

You can store how many items of given type you have in relation(through) model.

class BackpackItems(models.Model):
    backpack = models.ForeignKey(Backpack, on_delete = models.CASCADE)
    item = models.ForeignKey(Item, on_delete = models.CASCADE)
    quantity = models.PositiveIntegerField(default = 1)

class Item(models.Model):
    item_id = models.IntegerField(default=1, unique=True, primary_key=True)
    name = models.CharField(default="None", max_length=256)

class Backpack(models.Model):
    items = models.ManyToManyField(Item, through = BackpakItems)
👤Robert

Leave a comment