Model.objects.get() or None

How about this:

obj = Model.objects.filter(id=1).first()

now if no object with id=1 exists obj would be None

Ref: https://docs.djangoproject.com/en/3.2/ref/models/querysets/#django.db.models.query.QuerySet.first

Leave a Comment