1
 class Product(models.Model):

     name = models.CharField(max_length=50)
     desc = models.CharField(max_length=50)

     def __unicode__(self):
             return self.name


 class ProductModels(models.Model):
      product = models.ForeignKey(Product)
      name = models.CharField(max_length=50)
      price = IntegerField(max_length=50)

      def __unicode__(self):
             return self.name

 class Sold(model.Model)
        product = models.ForeignKey(Product)
        model_name = models.ForeignKey(ProductModels)
        sold_date = models.DateField()
        model_qty = models.IntegerField()

     def __unicode__(self):
             return self.sold_date

this is with reference to my previous question( that problem is solved) what i am trying to find out is how many models have been sold from Jan to Dec for the particular product.

so far i have done this, i know it's little crude. but it would help he if it written correctly:

views.py

    allmonths = ['jan' , ....., 'dec']
    products = Products.objects.all()
    for each_product in products :
        models = ProductModels.objects.filter(product = each_product)

        for each_model in models:
            for months in allmonths :
                att_name = str(each_model.model_name) + str(months)
                print 'attname %s' % att_name
                val = sold.objects.filter(product = each_product ,
                                          model_name = each_model ,(sold_date =(allmonths.indesx(month) + 1))) .aggregate(qty=Sum('model_qty'))
                val = temp(val['qty'])
                setattr(each_product, att_name , val)

I am not sure if this correct or not , but this approach has worked earlier. i used getattribute template tag from here.

This is how my template looks like :

{% for record in products %}
    <tr class="odd gradeX">
        <td><span class="label label-warning">{{ record.name }}</span></td>
        <td>
            <table class="table table-striped table-bordered table-hover" id="pipelineTbl">
                    {% for model in record.ProductModels_set.all %}
                    <tr class="odd gradeX">
                        <td>
                            <span class="label label-success">{{ model }}</span>
                        </td>
                    </tr>
                     {% endfor %}

            </table>
        </td>

        {% for months in allmonths %}
            <td>
                <table class="table table-striped table-bordered table-hover" id="pipelineTbl3">

                    {% for model in record.ProductModels_set.all %}
                        {% with model|add:months as val %}
                            <tr>
                                <td>{{ record|getattribute:val }}</td>
                            </tr>
                        {% endwith %}
                    {% endfor %}
                </table>
            </td>
        {% endfor %}
    </tr>
{% endfor %} 
Community
  • 1
  • 1
rgm
  • 1,241
  • 2
  • 16
  • 33
  • If you want to know how many models have been sold from January to December, does it mean you want to calculate how many products have been sold on a given year, right? – avenet Jan 27 '14 at 13:40

1 Answers1

1

You can make the following query:

product = some_product()
year = 2013

product_sells = Sold.objects.filter(product=product, sold_date__year=year)

If you want to know how many individual sells do this:

product_sells.count()

If you want to sum the product sold quantities:

from django.db.models import Sum
product_sells.Sum('model_qty')

In general avoid iterating over Products.objects.all(), as it's really expensive on database and memory.

avenet
  • 2,894
  • 1
  • 19
  • 26
  • I am getting the correct values, with my code, but the problem is it's not rendering in my html. I will definitely consider your code, but if you could help to find the problem in my template?? – rgm Jan 27 '14 at 13:54
  • 1
    Please consider I'm not using products anymore on this code. Are you passing the correct parameters to the template...what's the error? – avenet Jan 27 '14 at 13:57
  • I got the problem, there was no error, but a silly mistake , the block where string concat is being performed, i was accidentally passing integers, when i passed strings , i got the output, thanks. But then again as you said i will change my code. – rgm Jan 27 '14 at 14:28
  • one more thing, i might have forgotten to mention, i need results monthly, model wise(qty sold) for that particular model, can your approach suit. ? i think not, please correct me if i am wrong – rgm Jan 28 '14 at 03:49
  • Just add the month to this query: Sold.objects.filter(product=product, sold_date__year=year, sold_date__month=month) – avenet Jan 28 '14 at 15:33