Monday, 4 December 2023

Python django use filter.first() or try and catch to avoid record not found exception

 https://stackoverflow.com/questions/3090302/how-do-i-get-the-object-if-it-exists-or-none-if-it-does-not-exist-in-django

fitler.first() :

Content.objects.filter(name="baby").first()


try and catch():
try:
    go = SomeModel.objects.get(foo='bar')
except SomeModel.DoesNotExist:
    go = None

No comments:

Post a Comment