If my interpretation is correct, the ORM has an abstraction that you can use called get_or_create().
In your example, you could use:
Attendance.object.get_or_create(user=emp,date=enddate,crea_date=strdate)
|
Which will return a tuple with in the form (your_object, bool), where ‘your_object’ is the object returned from the db and ‘bool’ is the result of whether is was created (true) or retrieved (false). A more ideal way then to use this would be to pass multiple variables which python will unpack accordingly. Do this:
Attendance, created = Attendance.object.get_or_create(user=emp,date=enddate,crea_date=strdate)
|
For a deeper understanding try diving into the Django docs, there’s also a good post on this blog:
https://nsikakimoh.com/blog/learn-about-get_or_create-and-update_or_create.
Hope that helps.