tomkc
11/23/2016 - 5:30 PM

Django verify / validate in view if object exists

Django verify / validate in view if object exists

#orginal source: http://stackoverflow.com/questions/639836/what-is-the-right-way-to-validate-if-an-object-exists-in-a-django-view-without-r

try:
    listing = RealEstateListing.objects.get(slug_url=slug)
except RealEstateListing.DoesNotExist:
    listing = None
    
#another way:

if not RealEstateListing.objects.filter(slug_url=slug).exists():
    #do stuff...