jeudi 1 décembre 2016

Django exists() versues DoesNotExist

I have one question about django exists() and DoesNotExist. Example code:

id = 1
# first
if User.objects.get(pk=id).exists():
    # my logic
    pass
# second
try:
    User.objects.get(pk=id)
    # my logic
    pass
except User.DoesNotExist:
    return 0

I often user get() method, which practise is better ? which code is better ? first or second ? sorry for bad english.




Aucun commentaire:

Enregistrer un commentaire