已经使用其他查询集查看了查询

时间:2019-07-09 13:52:14

标签: django caching django-views

我按ajax request对学分列表进行排序。 第一次一切顺利,我得到了学分和update the cache的排序列表。但是,当我再次尝试对 进行排序时,出现错误。

  

已经使用不同的查询集查看了“ creditpayments”查询。您可能需要调整查询的顺序。

如果不更新缓存,则不会有错误。但是我需要更新它...

模型

class Credit(models.Model):
    pass


class CreditPayment(models.Model):
    credit = models.ForeignKey(Credit, on_delete=models.CASCADE, related_name='creditpayments')
    rate = models.DecimalField(_('rate'), max_digits=7, decimal_places=2)

观看次数

class SortedCreditsList(ListView):
    def get(self, *args, **kwargs):
        if self.request.is_ajax():
            credits = cache.get('credits')
            prefetch = Prefetch('creditpayments', CreditPayment.objects.all())
            credits = credits.prefetch_related(prefetch).annotate(min_rate=Min('creditpayments__rate'))
            credits = credits.order_by('min_rate')
            cache.set('credits', credits)
            credits = credits.filter(best=False, hot=False)

            template_ajax = render_to_string(
                    template_name='credits/includes/credits_filter_result.html',
                    context={
                        'credits': credits,
                    })

            return JsonResponse({'success': True, 'template': template_ajax})

跟踪

ValueError at /sorted_kredit/
'creditpayments' lookup was already seen with a different queryset. You may need to adjust the ordering of your lookups.

Traceback:  

File "/home/m0nte-cr1st0/.virtualenvs/finbee/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/m0nte-cr1st0/.virtualenvs/finbee/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/m0nte-cr1st0/.virtualenvs/finbee/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/m0nte-cr1st0/.virtualenvs/finbee/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/home/m0nte-cr1st0/work_projects/startup/finbee/credits/views.py" in get
  555.             cache.set('credits', credits)

File "/home/m0nte-cr1st0/.virtualenvs/finbee/local/lib/python2.7/site-packages/django/core/cache/backends/locmem.py" in set
  75.         pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)

File "/home/m0nte-cr1st0/.virtualenvs/finbee/local/lib/python2.7/site-packages/django/db/models/query.py" in __getstate__
  211.         self._fetch_all()

File "/home/m0nte-cr1st0/.virtualenvs/finbee/local/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  1076.             self._prefetch_related_objects()

File "/home/m0nte-cr1st0/.virtualenvs/finbee/local/lib/python2.7/site-packages/django/db/models/query.py" in _prefetch_related_objects
  656.         prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)

File "/home/m0nte-cr1st0/.virtualenvs/finbee/local/lib/python2.7/site-packages/django/db/models/query.py" in prefetch_related_objects
  1397.                                  "You may need to adjust the ordering of your lookups." % lookup.prefetch_to)

1 个答案:

答案 0 :(得分:2)

由您已经预取的模型导致的错误。

当您从缓存中获取.\Out值时,该对象已经被预提取。

const Model = require('path/to/model');

function findManyUsingPromise(model, query = {}, offset = 0, limit = 10) {

    return new Promise((resolve, reject) => {
        model.find(query, (error, data) => {
            if(error) {
                reject(error);
            }
            resolve({
                data,
                total: data.length || 0
            });
        }).skip(offset).limit(limit);
    });

}

// Call function

findManyUsingPromise(Model, {}, 0, 40).then((result) => {
    // Do something with result  {data: [object array], total: value }
}).catch((err) => {
    // Do something with the error
}); 

我认为这段代码可以使您的代码正常工作。