Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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###
Comment

PREVIOUS NEXT
Code Example
Python :: Python get first element from list 
Python :: remove extra blank spaces 
Python :: why pytest return No ModuleError 
Python :: Socket Programming Server Side 
Python :: python added dictionary together 
Python :: pyspark filter column in list 
Python :: Insert list element at specific index 
Python :: for char in string python 
Python :: current page django 
Python :: python how to delete a variable 
Python :: match case in python 
Python :: how many numbers greater than 100 using pytho 
Python :: how to get text of a tag in selenium python 
Python :: python regex validate phone number 
Python :: django test imagefield 
Python :: logical operators in python 
Python :: seaborn pandas annotate 
Python :: print to screen 
Python :: scikit learn library in python 
Python :: flask migrate multiple heads 
Python :: remove toggle/pandaslux 
Python :: python test class hashable 
Python :: telegram.ext package python 
Python :: django-admin startproject 
Python :: feature importance plot using lasso regression 
Python :: Add New Column to Pandas from Dictionary 
Python :: pytorch inverse 
Python :: form field required in django views 
Python :: df dtype 
Python :: Forbidden (CSRF token missing or incorrect.): /extension/stripe-pay/ WARNING 2021-06-01 13:45:22,532 log 408 140165573588736 Forbidden (CSRF token missing or incorrect.): /extension/stripe-pay/ 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =