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.

Make QuerySet .delete() return a success indicator or counter

See original GitHub issue

Hi, I want to check does the delete action is success, the filter().delete() response NoneType. Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
abondarcommented, Nov 22, 2018

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 from changes() and we would have to do SELECT 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)

1reaction
grigicommented, Nov 22, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

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