What is the best way to check if data is present in django?

django

Solution

If you need to use the object, use try/catch.

try:
    object = DemoModel.objects.get(id=8)
    #use object here
catch DoesNotExist:
    #catch stuff here

If you don't need to, just use exists().

if DemoModel.objects.filter(id=8).exists():
    #do stuff here

Problem

Using django, which is the "way" to check if data is present? I know i can either have a try/catch block on a get, or check the size of a `len` on a filter ie ``` try: DemoModel.objects.get(id=8) catch DoesNotExist: catch stuff here ``` or ``` if not len(DemoModel.objects.filter(id=8): do stuff here ``` i suppose i'm defining "best" as a. the standard way b. the more efficient way or is there no real difference? Or is there a non-partisan third way?

Original source