Comparing DateTimeField to datetime.now()

2,974 views
Skip to first unread message

Chris McComas

unread,
Jul 14, 2010, 11:43:05 AM7/14/10
to Django users
This is my model, I'm trying to set it so that if the game is in the
future, based on the field date, then to return True, if not return
False.

http://dpaste.com/218111/

I am importing datetime in my models.py but for some reason it's
giving me nothing. I tried displaying future and nothing shows up?
What have I done wrong?

Daniel Roseman

unread,
Jul 14, 2010, 11:49:31 AM7/14/10
to Django users
How/where are you calling this method?
--
DR.

jaymzcd

unread,
Jul 14, 2010, 11:54:53 AM7/14/10
to Django users
This code works fine for me:

In [1]: from gallery.models import Gallery
In [2]: g = Gallery.objects.all()[0]
In [3]: g.created_at
Out[3]: datetime.datetime(2010, 4, 1, 7, 11, 51)
In [4]: import datetime
In [5]: n = datetime.datetime.now()
In [6]: g.created_at > n
Out[6]: False
In [7]: [g.created_at, n]
Out[7]:
[datetime.datetime(2010, 4, 1, 7, 11, 51),
datetime.datetime(2010, 7, 14, 8, 53, 22, 960271)]

Are you sure your self.date has been initialized and isn't None?



On Jul 14, 4:43 pm, Chris McComas <mccomas.ch...@gmail.com> wrote:

Chris McComas

unread,
Jul 14, 2010, 11:55:05 AM7/14/10
to Django users
I'm using it in my template...

Basically this http://dpaste.com/218114/

Mainly I want to use it for the {% if %} but I tried to just show it
was well and still nothing...

Subhranath Chunder

unread,
Jul 14, 2010, 11:57:56 AM7/14/10
to django...@googlegroups.com
How did u import the datetime module?
If you did:

a>
import datetime
Then, use 'datetime.datetime.now()' instead of 'datetime.now()'

b>
from datetime import datetime
Then 'datetime.now()' should work correctly.

Thanks,
Subhranath Chunder.




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Chris McComas

unread,
Jul 14, 2010, 1:29:45 PM7/14/10
to Django users
This worked:

import datetime
Then, use 'datetime.datetime.now()' instead of 'datetime.now()'

Thanks guys!

On Jul 14, 11:57 am, Subhranath Chunder <subhran...@gmail.com> wrote:
> How did u import the datetime module?
> If you did:
>
> a>
> import datetime
> Then, use 'datetime.datetime.now()' instead of 'datetime.now()'
>
> b>
> from datetime import datetime
> Then 'datetime.now()' should work correctly.
>
> Thanks,
> Subhranath Chunder.
>
> On Wed, Jul 14, 2010 at 9:13 PM, Chris McComas <mccomas.ch...@gmail.com>wrote:
>
>
>
> > This is my model, I'm trying to set it so that if the game is in the
> > future, based on the field date, then to return True, if not return
> > False.
>
> >http://dpaste.com/218111/
>
> > I am importing datetime in my models.py but for some reason it's
> > giving me nothing. I tried displaying future and nothing shows up?
> > What have I done wrong?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-users...@googlegroups.com<django-users%2Bunsubscribe@google groups.com>
> > .
Reply all
Reply to author
Forward
0 new messages