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.

QuerySet first() method gives Cannot filter a query once a slice has been taken.

See original GitHub issue

Hey guys!

It seems that this code change gave me an issue, as following:

User.objects.filter(email="paul@email.nl").first()

Gives me:

File "/Users/pauloostenrijk/webprojects/app/app/users/import_export/resources.py", line 57, in before_import
user = User.objects.all().filter(email=row['email']).first()
File "/Users/pauloostenrijk/.virtualenvs/app/lib/python2.7/site-packages/django/db/models/query.py", line 550, in first
objects = list((self if self.ordered else self.order_by('pk'))[:1])
File "/Users/pauloostenrijk/.virtualenvs/app/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
self._fetch_all()
File "/Users/pauloostenrijk/.virtualenvs/app/lib/python2.7/site-packages/safedelete/queryset.py", line 121, in decorator
self._filter_visibility()
File "/Users/pauloostenrijk/.virtualenvs/app/lib/python2.7/site-packages/safedelete/queryset.py", line 98, in _filter_visibility
"Cannot filter a query once a slice has been taken."
AssertionError: Cannot filter a query once a slice has been taken.

Here are some implementation details:

from safedelete.managers import SafeDeleteManager

class UserManager(BaseUserManager, SafeDeleteManager):
@python_2_unicode_compatible
class User(SafeDeleteModel, AbstractBaseUser, TimeStampedUUIDModel, PermissionsMixin):
    objects = UserManager()

Version: 0.4.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:20 (14 by maintainers)

github_iconTop GitHub Comments

4reactions
Gagarocommented, Jun 19, 2017

0.4.2 has been released with the fix.

3reactions
Gagarocommented, Jan 4, 2018

Thanks for the report, I fixed it in master but I don’t have the time to do a release right now. It will be for next week now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

django - "Cannot filter a query once a slice has been taken"
Slicing an unevaluated QuerySet usually returns another unevaluated QuerySet, but Django will execute the database query if you use the "step" ...
Read more >
filtering a QuerySet after a slice - Google Groups
AssertionError: Cannot filter a query once a slice has been taken. I would think that it should be possible as long as the...
Read more >
Source code for django.db.models.query
assert self.query.can_filter(), \ "Cannot update a query once a slice has been taken." self._for_write = True query = self.query.clone(sql.
Read more >
QuerySet API reference — Django 4.1.4 documentation
A QuerySet is iterable, and it executes its database query the first time you iterate over ... Django provides a count() method for...
Read more >
I wanna paginate in class view, but an error happened ...
... but an error happened "Cannot filter a query once a slice has been taken. ... method or queryset attribute. don't do it...
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