使用setattr在模型上设置的查询

时间:2014-01-27 13:26:19

标签: python django django-models django-templates

 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

这是参考我之前的问题(问题已解决) 我想知道的是how many models have been sold from Jan to Dec for the particular product

到目前为止,我已经做到了这一点,我知道这有点粗糙。但如果写得正确,它会有所帮助:

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)

我不确定这是否正确,但这种方法更早发挥作用。我使用了来自here的getattribute模板标签。

这就是我的模板的样子:

{% 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 %} 

1 个答案:

答案 0 :(得分:1)

您可以进行以下查询:

product = some_product()
year = 2013

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

如果你想知道有多少个人出售:

product_sells.count()

如果您想要将产品销售数量相加:

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

通常避免迭代Products.objects.all(),因为它在数据库和内存上非常昂贵。