Answers for "how to check with if an object exists in queryset django"

5

check django object exists

modelName.objects.filter(pk='id').exists()
Posted by: Guest on May-04-2020
0

how to check if given primary key exists in django model

If using the object ---
try:
   obj = model1.objects.get(pk=20)
except DoesNotExist:
   ###do something####
    
Else ----
if model1.objects.filter(pk=20).exists():
   ###do something###
else:
  ###do otherthing###
Posted by: Guest on April-29-2020

Code answers related to "how to check with if an object exists in queryset django"

Python Answers by Framework

Browse Popular Code Answers by Language