Yax
Yax

Reputation: 2189

Django __gt filter return the duplicate item

I want to fetch items from my model by using time_created as criteria.

If the latest item I fetched was posted at 12:45:44, I store it in request.session['time_at'] = 12:45:44 and use it to fetch item that are later than the last fetched.

new_notice = Notify.objects.filter(time_created__gt = request.session['time_at'])

This is supposed to return items with time from 12:45:45 but it still return the ones with 12:45:44 which is making me have duplicate of items I have already fetched.

How do I deal with this the right way?

Upvotes: 1

Views: 176

Answers (2)

GwynBleidD
GwynBleidD

Reputation: 20539

Django is storing time in database with more precision, than seconds. So if you time_at holds exact time of 12:45:44, query for database will retrieve fields that have time more than that just by miliseconds, so time of field retrieved from database can have 12:45:44.231 and it will be retrieved.

Upvotes: 0

ezdookie
ezdookie

Reputation: 1677

Convert 12:45:45 (string) to datetime:

import datetime
time_at = datetime.datetime.strptime(request.session['time_at'], '%H:%i:%s')
new_notice = Notify.objects.filter(time_created__gt = time_at)

Upvotes: 1

Related Questions