0

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?

Uma
  • 689
  • 2
  • 12
  • 35

1 Answers1

0

You have a clear understanding on how you want to do it.

I would agree with you about reusing the same weights for different variations rather than creating new ones which would again have same weights. This is what I think that would be better there may be multiple ways to do it.

To answer your question, please try this Model relations in your app:

 class Quantity(models.Model):
     quantity = models.PositiveIntegerField()

 class Weight(models.Model):
     weight = models.PositiveIntegerField()
     quantity = models.ManyToManyField(Quantity)


 class Variation(models.Model):
     name = models.CharField()
     weight = models.ManyToManyField(Weight)

Then add all the weights as you require in the Weights class individually. So after than whenever you need to add some weight to Variation table then you can select the weights from the Weights class in which we have already added the weights that we might require.

In this way you can reuse the same weight for different variations without even having to have duplicate records.

Make sure you have registered both the models in the admin for easy access.

This should solve your problem for having multiple records in the weight table.

kt14
  • 838
  • 15
  • 25
  • thanks for responding. This does not entirely solve the problem since with each weight I have to set quantity as well. In the above scenario each instance of variation model can have multiple weights but can only have one quantity whereas I also need to have quantity based on weight options. – Uma Aug 07 '16 at 20:21
  • You can create a two different relations for weight and quantity. I have updated the answer. – kt14 Aug 07 '16 at 20:30