QuerySet first() method gives Cannot filter a query once a slice has been taken.
See original GitHub issueHey 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:
- Created 6 years ago
- Reactions:6
- Comments:20 (14 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
0.4.2 has been released with the fix.
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.