I'm using Django to create a part inventory where I work. Here is a snippet of the models I have:
class Part(models.Model):
description = models.CharField(max_length=64)
number= models.CharField(max_length=64)
price= models.FloatField()
class Group(models.Model):
name = models.CharField(max_length=64)
parts = models.ManyToManyField(Part)
So I have different groups (orders) with some parts in it.
What I want to do is to have a quantity property for the parts of my group. But if I want to add the quantity field to my Part object, each group will have the same quantity, which is not the correct behavior. How can I have my groups remember how much of each part they have?
Thanks for your input and I hope this is not a total noob question!
You would need a through table:
The intermediate model is associated with the ManyToManyField using the through argument to point to the model that will act as an intermediary.
class Part(models.Model):
description = models.CharField(max_length=64)
number= models.CharField(max_length=64)
price= models.FloatField()
class Group(models.Model):
name = models.CharField(max_length=64)
parts = models.ManyToManyField(Part, through='GroupPart')
Create a new through table between Group
and Part
that holds the quantity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With