If I had to rephrase your question, it appears that you might want to: 1. Query your Attendance model for some specific object. 2. If the object is not present, create it.
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. On Feb 18, 2023, 8:01 AM -0500, django-users@googlegroups.com, wrote: > django-users@googlegroups.com Google Groups > Topic digest > View all topics > > • Orm query - 1 Update > > Orm query > Prashanth Patelc <prashanthpat...@gmail.com>: Feb 17 06:40PM +0530 > > Hi all, > > This is my model if model contain previous month dates I need to and store > the data If user is new previous data is not available I need to store > directly to db . > How to write orm query? > > > Attendance.object.filter(user=emp,date=enddate,crea_date=strdate) > > > > If dates is not available in attendance model how to write orm query? > Back to top > You received this digest because you're subscribed to updates for this group. > You can change your settings on the group membership page. > To unsubscribe from this group and stop receiving emails from it send an > email to django-users+unsubscr...@googlegroups.com. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bc7619c2-2b4e-4bb7-b158-fd682bc4c6ac%40Spark.