简体   繁体   中英

How to check for an erroneous primary key parameter in a filtered django ORM query?

Let's say I'm given a list of primary keys of model instances on which I'm to perform an operation (eg delete() ). I could do:

Widgets.objects.filter(pk__in=keys).delete()

However, then I wouldn't know if there were erroneous pks in keys that wouldn't have been caught by the filter.

What's the most efficient way to check that all the pks are legitimate before performing the operation? len(keys) == Widgets.objects.filter(pk__in=keys).count() ?

The first parameter of the tuple returned from delete is the number of rows deleted. You can compare that with the length of the keys list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM