Make QuerySet .delete() return a success indicator or counter
See original GitHub issueHi, I want to check does the delete action is success, the filter().delete()
response NoneType
. Thanks.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:6 (4 by maintainers)
Top Results From Across the Web
python - Deleting Object from QuerySet List in Django with ...
1 Answer 1 ; try: List.objects.get(pk).delete() except List.DoesNotExist: do_something_when_item_does_not_exist() return redirect("some_url").
Read more >Making queries | Django documentation
The QuerySet returned by all() describes all objects in the database table. Usually, though, you'll need to select only a subset of the...
Read more >QuerySet API reference — Django 4.1.4 documentation
values() Returns a QuerySet that returns dictionaries, rather than model instances, when used as an iterable. Each of those dictionaries represents an object, ......
Read more >Using Django querysets effectively - And then it crashed
To avoid populating the queryset cache, but to still iterate over all your results, use the iterator() method to fetch the data in...
Read more >Django select_related and prefetch_related - Chand's Blog
return self.name class Book(models.Model): ... Publisher.objects.all().delete() ... create 10 stores and insert 10 books in every store
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
Well, the fact that there is no exception raised is success message by itself.
Regarding returning something useful: Problem is - standard SQL doesn’t provide anything to get meaningful messages from deleting query.
So if we would want to implement such thing we would have to implement custom solution for each of backends (
RETURNING
for postgres, for sqlite we could try fetching something fromchanges()
and we would have to doSELECT ROW_COUNT()
for MySQL) For now I am not sure it’s really needed, and I am quite sure that it’s shouldn’t be default behavior because it adds overhead that is not required for most of users.If we would add it I think interface should be something like this:
Event.filter(...).delete(return_details=True)
It is a good suggestion. Right now it only ever returns
None
, but returning a success/count would be better. It doesn’t make sense to return the deleted objects, as they now longer exist.