1

I have a model with a field name 'timestamp' that is a datetime field, and when I print them in python manage.py shell I get:

>>> a.timestamp
datetime.datetime(2013, 7, 15, 18, 45, tzinfo=<UTC>)

Now.... I am running django celery task every 30 seconds that suppose to retrieve all the matching objects in the last 30 seconds.

how can I do it (thing = MyModelName.objects.filter(.. something ..)

I hope that I manage to explain my self.

Thank you all in advanced.

Update: When I am typing in my server (python shell)

datetime.datetime.now() I am getting

datetime.datetime(2013, 7, 15, 20, 34, 4, 366166)

2
  • What exactly are you having trouble with? Commented Jul 15, 2013 at 19:46
  • Just updated my question. The problem that I am having is that I am not getting the matching objects. maybe it has something to do with timezone? Commented Jul 15, 2013 at 20:35

2 Answers 2

2
MyModelName.objects.filter(timestamp__gte=(datetime.datetime.now() - datetime.timedelta(seconds=30)))
Sign up to request clarification or add additional context in comments.

2 Comments

With your code I am getting all the object every 30 seconds, which is not good. :( Thank you very much for your help
That shouldn't be the case - but you could try django.utils.timezone.now() instead of datetime.datetime.now(). docs.djangoproject.com/en/dev/ref/utils/…
1
import datetime
from datetime import timedelta

time = datetime.datetime.now()
delta = timedelta(seconds=30)
newtime = time + delta #after adding 30 sec with current time

so you can loop through the (newtime - time) time interval then  you can
get the expected result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.