How to use 'python if object exists' in Python

Every line of 'python if object exists' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
90def object_exists(model_object):
91 """
92 Check if a model_object already exists.
93 :param model_object: an instance of a model
94 :return: true if the object already exists
95 """
96 from base.models import student, tutor, person
97
98 if model_object.object.__class__ == person.Person:
99 global_id = model_object.object.global_id
100 return model_object.object.__class__.objects.filter(global_id=global_id).exists()
101 elif model_object.object.__class__ == student.Student:
102 registration_id = model_object.object.registration_id
103 return model_object.object.__class__.objects.filter(registration_id=registration_id).exists()
104 elif model_object.object.__class__ == tutor.Tutor:
105 external_id = model_object.object.external_id
106 return model_object.object.__class__.objects.filter(external_id=external_id).exists()
107 return True
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code


Related snippets