What's the best way to check if an object is part of ManyToMany relationships in Django

from a site instance with the ManyToMany relationship to the kiosk, I would like to check if the kiosk object is part of the relationship.

I could do

self.apps.get(id=app_id).exists() and check if True

or

self.apps.get(id=app_id) and catch the ObjectDoesNotExist error

or

self.apps.filter(id=app_id) and check if True
  • If I need to catch a possible ObjectDoesNotExist error, I can also use the second
  • I can do the second one, but it doesn't seem super clean.
  • can use the third, but using a filter by a unique identifier seems wrong to me.

You can tell me to use everything that works, and that will be a valid answer; -)

+5
source share
1 answer

I would use

self.apps.filter(id=app_id).exists()

What is wrong with that?

+10
source

All Articles