简体   繁体   中英

Django: Queryset not saving changes to value?

Here, result is a Queryset. I am going through it and updating the value for it's price attribute to match what I get from the price_map.price value in my second query to Product.objects .

This works and updates the prices accordingly. Until it goes into the .order_by method. Then, all of the values of price return to what they originally were.

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

If I add in some print statements for just before and just after the .order_by call, here is the output I get:

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

Stack trace for the issue when I try to save the item after changing the price value in the for loop:

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

order_by function returns a new queryset instance. Because of the lazy nature of queryset, the new result queryset (with order_by ) will execute another query to fetch data from the database.

Since you are not saving the updated item, the value is just updated at the python object level and not saved in the database. So when you are accessing the price with the new result queryset, it shows the result fetched from the database which is still the older one.

So to solve the issue, either sort the objects at python level or save the items before calling the order_by function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM