【问题标题】:Django - Add field to queryset to store computation resultsDjango - 向查询集添加字段以存储计算结果
【发布时间】:2012-08-20 13:42:45
【问题描述】:

我是 Django 的新手,来自 PHP 世界。我试图在计算完之后将一个字段“添加”到查询集中,但不知道该怎么做。在 PHP 中,我只需在数组中添加一列并将我的东西存储在其中。

这是我的代码:

def (id):
    mystuff_details    = mystuff_details.objects.filter(stuff_id=id)
    newthing = '';
    for mystuff in mystuff_details:
        newthing_lists = //some query to get another queryset
        for newthing_list in newthing_lists:
            newthing = newthing_list.stuffIwant
            //Here I want to make some computation, and ADD something to newthing, let's say:  
            to_add = (mystuff.score+somethingelse)
            //I've heard about the .append but I'm sure I'm screwing it up
            newthing.append(to_add)

所以基本上在我的模板中我希望能够调用: {% for newthings_list 中的新事物 %} {{ newthing.to_add }} {% 结束 %}

TL;DR:我基本上想从我的数据库中检索一个东西列表,并在这个对象列表中添加一个包含计算值的字段。

如果不清楚,请告诉我,我很难从 php 切换到 django 哈哈。

谢谢!

编辑:

所以,我正在尝试使用字典,但我一定错过了逻辑:

def (id):
    mystuff_details    = mystuff_details.objects.filter(stuff_id=id)
    newthing = {};
    for mystuff in mystuff_details:
        newthing_lists = //some query to get another queryset
        for newthing_list in newthing_lists:
            //Newthing_list can have several times the same I, and the scores need to add up
            if newthing[newthing_list.id] > 0: //This doesn't seem to work and throws an error (KeyError)
                newthing[newthing_list.id] = newthing[newthing_list.id] + some_calculated_thing
            else: 
                newthing[newthing_list.id] = some_calculated_thing

然后当我得到它的工作时,我不知道如何在模板中访问它:

 {% for id in my_list %}
     {{newthing[id]}} ? Or something like newthing.id ?
 {% end %}

谢谢!

【问题讨论】:

    标签: django list append


    【解决方案1】:

    如果@Melvyn 的解决方案不适合您:

    确保在修改查询集时不要在查询集上调用.all() 等,因为.all() 返回查询集的副本,例如

    qs = foo.objects.filter(bar=1); 
    for obj in qs.all(): 
        obj.caz = 1;  # Will not persist
    

    现在qs 将保持不变。

    而是:

    qs = foo.objects.filter(bar=1).all(); 
    for obj in qs: 
        obj.caz = 1; 
    

    现在您可以在模板中访问.caz

    【讨论】:

      【解决方案2】:

      为什么不用字典?

      newthing = {}
      newthing['your_key'] = to_add
      

      在模板中,您可以通过以下方式访问字典值:

      {{newthing.your_key}}
      

      如果您有字典,请使用 for 循环

      【讨论】:

      • 您好,谢谢!我尝试使用字典,这是我的问题中“编辑”之后的部分,但现在还没有运气。我的代码做错了什么?谢谢!
      • 首先,查看writing views in django。在您看来,您必须返回类似return render_to_response('my_template.html', my_data_dictionary, context_instance=RequestContext(request))
      【解决方案3】:

      您可以在 python 对象上设置任何内容:

      for obj in self.model.objects.all() :
          obj.score = total_score / total_posts
      

      即使 obj 没有 score 属性,这也会起作用。在模板请求中是这样的:

      {{ obj.score }}
      

      是的,就是这么简单。 但是,如果您正在做的计算可以在数据库中完成,您应该查看annotate

      【讨论】:

      • 我知道有些人可能会说这太简单了,但这可能是我见过的最简单的解决方案。
      • @Melvyn 这是将计算字段添加到查询集的方法
      猜你喜欢
      • 1970-01-01
      • 2021-04-02
      • 2016-11-01
      • 2017-09-21
      • 2010-12-11
      • 2012-01-26
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多