question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValueError('Prefetch querysets cannot use values().')

See original GitHub issue

Problem

When I upgraded to version 3.0.0 I started getting the following error ValueError('Prefetch querysets cannot use values().')

Here is the full traceback:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/.virtualenvs/tcj/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/.virtualenvs/tcj/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/.virtualenvs/tcj/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/.virtualenvs/tcj/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Projects/tcj/mainsite/management/commands/cron_hook.py", line 412, in handle
    getattr(CronFunctions, task_name)(*args)
  File "/Projects/tcj/mainsite/management/commands/cron_hook.py", line 119, in elasticsearch_reindex
    tcj_elasticsearch.reindex_listings(start_id=start_id, end_id=end_id)
  File "/Projects/tcj/TCJ/services/one_offs.py", line 452, in do_func
    return func(*args, **kwargs)
  File "/Projects/tcj/TCJ/tcj_elasticsearch.py", line 1602, in reindex_listings
    listings = BaseListing.listings.with_prefetches().using(db).indexable()\
  File "/Projects/tcj/business/models/listings.py", line 419, in with_prefetches
    ), to_attr='active_student_responses_prefetched'),
  File "/.virtualenvs/tcj/lib/python2.7/site-packages/django/db/models/query.py", line 1312, in __init__
    raise ValueError('Prefetch querysets cannot use values().')
ValueError: Prefetch querysets cannot use values().

I don’t understand the error given this feature from almost a year ago.

Environment

  • Django Model Utils version: 3.0.0
  • Django version: 1.11.2
  • Python version: 2.7.13

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
hanleyhansencommented, Jun 22, 2017

I think I found the issue:

class InheritanceIterable(BaseIterable):
    def __iter__(self):
        queryset = self.queryset
        iter = ModelIterable(queryset)
        if getattr(queryset, 'subclasses', False):
            extras = tuple(queryset.query.extra.keys())
            # sort the subclass names longest first,
            # so with 'a' and 'a__b' it goes as deep as possible
            subclasses = sorted(queryset.subclasses, key=len, reverse=True)
            for obj in iter:
                sub_obj = None
                for s in subclasses:
                    sub_obj = queryset._get_sub_obj_recurse(obj, s)
                    if sub_obj:
                        break
                if not sub_obj:
                    sub_obj = obj

                if getattr(queryset, '_annotated', False):
                    for k in queryset._annotated:
                        setattr(sub_obj, k, getattr(obj, k))

                for k in extras:
                    setattr(sub_obj, k, getattr(obj, k))

                yield sub_obj
        else:
            for obj in iter:
                yield obj

InheritanceIterable should inherit from ModelIterable not BaseIterable. Hotpatching this works for me locally.

0reactions
jarshwahcommented, Apr 13, 2018

@hanleyhansen yep I know, just highlighting the PR against the issue a little more.

This is a jazzband project, which means there are a bunch of maintainers that contribute to various projects under the jazzband umbrella. It’s my first time interacting with one though!

There are a bunch of issues and PRs open. Maybe helping out on some of the other items would help to encourage some attention on this particular issue 🤷‍♀️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prefetch querysets cannot use raw(), values(), and values_list ...
Django: ValueError: Prefetch querysets cannot use raw(), values(), and values_list() - Stack Overflow. Stack Overflow for Teams – Start collaborating and ...
Read more >
Allow Prefetch query to use .values() - Django's bug tracker
The error seems obvious: the prefetch mechanism tries to access to attributes for some reason, but it got a dict from the query,...
Read more >
django.db.models.query — Parkour LIMS documentation
_iterable_class, ModelIterable): raise ValueError('Prefetch querysets cannot use values().') if to_attr: self.prefetch_to ...
Read more >
django.db.models.query — Festival Grid 0.1 documentation
def __iter__(self): queryset = self.queryset db = queryset.db compiler ... ModelIterable): raise ValueError('Prefetch querysets cannot use values().
Read more >
https://www.doc.ic.ac.uk/project/2012/wmproject201...
assert self.query.can_filter(), \ "Cannot use 'limit' or 'offset' with ... in the current QuerySet, setting all the given fields to the appropriate values....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found