Django:Queryset没有保存对值的更改吗?

时间:2018-02-16 17:49:00

标签: python django django-queryset

此处,result是一个Queryset。我正在浏览并更新它的price属性的值,以匹配我从第二个查询中的price_map.price值到Product.objects的值。

这可以相应地更新价格。直到它进入.order_by方法。然后,price的所有值都返回到原来的值。

if sort_by in valid_sorts:
            for item in result:
                retrieved_item = Product.objects.get(name=item.name).get_pricing_info(self.request.session.get('visitors_country'))
                if retrieved_item['price_map'] is None:
                    print 'we got a none here! ', item.name
                else:
                    item.price = retrieved_item['price_map'].price

            if sort_by == 'highlow':
                result = result.order_by('-price')
            elif sort_by == 'lowhigh':
                result = result.order_by('price')
            elif sort_by == 'newest':
                result = result.order_by('-date','-price')
            elif sort_by == 'relevancy':
                pass

如果我在.order_by调用之前和之后添加一些打印语句,这是我得到的输出:

Just before the order!
79.99
119.99
99.99
69.99
119.99
89.99
69.99
69.99
99.99
44.99
599.99
599.99
69.99
69.99
69.99
69.99
249.99
799.99
119.99
139.99
199.99
249.99
139.99
149.99
139.99
69.99
139.99
199.99
69.99
29.99
0.0
139.99
34.99
54.99
119.99
149.99
69.99
69.99
89.99
119.99
119.99
149.99
149.99
1699.99
69.99
249.99
39.99
39.99
39.99
599.99
999.99
199.99
49.99
119.99
119.99
249.99
99.99
99.99
199.99
69.99
99.99
39.99
169.99
this is the high low sort!
1499.99
599.99
599.99
399.99
349.99
299.99
249.99
209.99
199.99
199.99
179.99
159.99
159.99
129.99
129.99
119.99
119.99
119.99
119.99
119.99
99.99
99.99
89.99
89.99
89.99
69.99
69.99
69.99
69.99
59.99
59.99
49.99
49.99
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0

在我在for循环中更改价格值后尝试保存项目时,问题的堆栈跟踪:

Traceback (most recent call last):
  File "/home/user/.virtualenvs/rf/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/home/user/.virtualenvs/rf/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/user/.virtualenvs/rf/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/user/.virtualenvs/rf/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/user/.virtualenvs/rf/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/user/.virtualenvs/rf/local/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/user/.virtualenvs/rf/local/lib/python2.7/site-packages/haystack/generic_views.py", line 120, in get
    form = self.get_form(form_class)
  File "/home/user/.virtualenvs/rf/local/lib/python2.7/site-packages/django/views/generic/edit.py", line 45, in get_form
    return form_class(**self.get_form_kwargs())
  File "/home/user/.virtualenvs/rf/local/lib/python2.7/site-packages/haystack/generic_views.py", line 94, in get_form_kwargs
    kwargs = super(FacetedSearchMixin, self).get_form_kwargs()
  File "/home/user/.virtualenvs/rf/local/lib/python2.7/site-packages/haystack/generic_views.py", line 65, in get_form_kwargs
    kwargs.update({'searchqueryset': self.get_queryset()})
  File "/home/user/Documents/sandbox/opt/rock/ro/rf/products/views.py", line 344, in get_queryset
    item.save()
TypeError: 'NoneType' object is not callable

1 个答案:

答案 0 :(得分:2)

order_by函数返回一个新的queryset实例。由于queryset的惰性,新的result查询集(带有order_by)将执行另一个查询以从数据库中获取数据。

由于您没有保存更新的项目,因此该值仅在python对象级别更新,而不是保存在数据库中。因此,当您使用新的price查询集访问result时,它会显示从数据库中获取的结果,该结果仍然是较旧的数据库。

因此,要解决此问题,请在python级别对对象进行排序,或者在调用order_by函数之前保存项目。