【问题标题】:query set on model with setattr使用 setattr 在模型上设置查询
【发布时间】:2014-02-18 08:46:29
【问题描述】:
 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 月到 12 月销售了多少型号,是否意味着您要计算给定年份销售了多少产品,对吗?

标签: python django django-models django-templates


【解决方案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(),因为它在数据库和内存上确实很昂贵。

【讨论】:

  • 我的代码得到了正确的值,但问题是它没有在我的 html 中呈现。我肯定会考虑你的代码,但如果你能帮助在我的模板中找到问题吗??
  • 请考虑我不再在此代码上使用产品。您是否将正确的参数传递给模板...错误是什么?
  • 我得到了问题,没有错误,但是一个愚蠢的错误,正在执行字符串 concat 的块,我不小心传递了整数,当我传递字符串时,我得到了输出,谢谢。但话又说回来,正如你所说,我将更改我的代码。
  • 还有一件事,我可能忘记提及了,我需要每个月的结果,该特定模型的模型明智(售出数量),您的方法是否适合。 ?我认为不是,如果我错了,请纠正我
  • 只需将月份添加到此查询中:Sold.objects.filter(product=product, sold_date__year=year, sold_date__month=month)
猜你喜欢
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 2013-08-11
相关资源
最近更新 更多