how to check if given primary key exists in django model

Solutions on MaxInterview for how to check if given primary key exists in django model by the best coders in the world

showing results for - "how to check if given primary key exists in django model"
Jacopo
20 Mar 2016
1If using the object ---
2try:
3   obj = model1.objects.get(pk=20)
4except DoesNotExist:
5   ###do something####
6    
7Else ----
8if model1.objects.filter(pk=20).exists():
9   ###do something###
10else:
11  ###do otherthing###