[Answered ]-OneToOne Field django

2👍

✅

You want to check if an entry_history has been added for the specific entry and related.model.DoesNotExist means that it does not exists, so the exception is an answer to your question.

entry = Entry.objects.get(pk=4)
try:
    entry_history = entry.history
    # if this code runs it means the object exists
except entry.history.model.DoesNotExist:
    # it does not exist

Alternatively you can use count:

entry = Entry.objects.get(pk=4)
if entry.history.count():
    # it does exist

Leave a comment