[Python] How do I get the object if it exists, or None if it does not exist in Django?

Discussão em 'Python' iniciado por Stack, Janeiro 29, 2025.

  1. Stack

    Stack Membro Participativo

    When I ask the model manager to get an object, it raises DoesNotExist when there is no matching object.

    go = Content.objects.get(name="baby")


    Instead of DoesNotExist, how can I have go be None instead?

    Continue reading...

Compartilhe esta Página