masterofdestiny
masterofdestiny

Reputation: 2811

Comparing date in django queryset

I have a query string which i am using to get all archived (passed) meetings

like

 get_meetings =  Meeting.objects.filter(created_by = user_id.id,meeting_datetime__lte =  datetime.now())

here is the model structure

 meeting_datetime = models.DateTimeField(default=datetime.datetime.now,blank=True, null=True)

the above query is returning all meetings which is going to be happen on 29 december also .

I want the details of all meetings before now .

PLease tell me what i am doing wrong here >

Upvotes: 1

Views: 14741

Answers (2)

dani herrera
dani herrera

Reputation: 51655

To get strict condition you should use less than __lt instead "less equal than". Also, to avoid include meetings day, you can get "first minute day":

today_min = datetime.datetime.combine(date.today(), datetime.time.min)
get_meetings = ( Meeting
                .objects
                .filter(created_by = user_id.id,
                        meeting_datetime__lt =  today_min
                       )
               )

Upvotes: 4

Aamir Rind
Aamir Rind

Reputation: 39659

lte means less than or equal to you need lt which means less than

get_meetings =  Meeting.objects.filter(created_by__id = user_id.id,
                                       meeting_datetime__lt=datetime.now())

Upvotes: 0

Related Questions