Re: Digest for django-users@googlegroups.com - 1 update in 1 topic

30 views
Skip to first unread message

Ryan Austin

unread,
Feb 18, 2023, 9:51:18 AM2/18/23
to django...@googlegroups.com
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...@googlegroups.com, wrote:
Prashanth Patelc <prashan...@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?
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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages