I have a simple eCommerce site with Product and Variation.
Each variation is definitely going to have different weights, and each weight will have a quantity.
I have implemented a Weight model with a ForeignKey relationship to Variation since each variation can have different weights and each weight will have a quantity.
class Weight(models.Model):
variation = models.ForeignKey(Variation)
size = models.DecimalField(decimal_places=3, max_digits=8,
validators=[MinValueValidator(Decimal('0.10'))])
quantity = models.PositiveIntegerField(null=True, blank=True, help_text="Select Quantity for this size")
I added weight as inline and can add multiple weight values and quantity in a variation. Please see this https://i.stack.imgur.com/drZdy.jpg
One might think this could be possible through creating variation for each weight but since it is definite that each product will have different weights there is no need create a variation for each weight.
Now the problem I am facing is that each variation will have different weights so for e.g. a variation could have weights of 1lb, 2 lb, 3lb. Each of these will create new weight objects for each variation. This means if another variation also has weights of 1lb, 2 lb, 3lb, new objects are created and NOT the existing ones are reused. This will result in a huge db table with many duplicates weight values. This is a problem because there is a limited number of weight and quantity value needed by any product (weight = 1lb to 100lb and quantity = 1 to 100) and so these should ideally be reused.
To avoid this I am thinking to have the Weight model with ManyToMany field to Variation and then quantity should be dropdown for each selected weight. This will allow be to store values of both weight and quantity, and have each product use the same values in each instance.
The problem I have is:
1. Is this the correct approach?
2. if not what is the best approach to do this?
3. If this is the correct approach how to do this?
4. If this is the correct approach how do I display this in admin site since each weight should also have a quantity (I have no clue how to do this)?
5. Is there a better way to achieve this, and if so how?