Discussion:
can't compare datetime.datetime to datetime.date
Rafał Szymański
2014-01-27 13:38:20 UTC
Permalink
Hi again

I wolud like ask another question.
If someone would be so kind and help that will be great.
I'm in the begining of django tutorial.

I use python3.3 and django1.6

I get error:
can't compare datetime.datetime to datetime.date

Here is my code:

import datetime
from django.utils import timezone
from django.db import models

class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateField('date published')
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
def __str__(self):
return self.question

class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text

Thanks in advance
Rafał
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0a985ef1-4b69-44d3-9523-c3e1ab23c8e0%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
i***@simpler.co
2014-01-27 14:43:59 UTC
Permalink
You're comparing pub_date, which is a datetime.date, to timezone.now(),
which is a datetime.datetime. Check the docs
at http://docs.python.org/2/library/datetime.html for more.

In order to convert timezone.now() to a date, just call
timezone.now().date(), like:

In [1]: from django.utils import timezone

In [3]: timezone.now()

Out[3]: datetime.datetime(2014, 1, 27, 14, 42, 29, 408491, tzinfo=<UTC>)

In [4]: timezone.now().date()

Out[4]: datetime.date(2014, 1, 27)
Post by Rafał Szymański
Hi again
I wolud like ask another question.
If someone would be so kind and help that will be great.
I'm in the begining of django tutorial.
I use python3.3 and django1.6
can't compare datetime.datetime to datetime.date
import datetime
from django.utils import timezone
from django.db import models
question = models.CharField(max_length=200)
pub_date = models.DateField('date published')
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
return self.question
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
return self.choice_text
Thanks in advance
Rafał
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a91997c8-faea-49b3-a75f-2a32e81bf9ca%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Rafał Szymański
2014-01-28 10:41:28 UTC
Permalink
Thank You so much.
Best wishes
Rafal

W dniu poniedziałek, 27 stycznia 2014 15:43:59 UTC+1 uÅŒytkownik
Post by i***@simpler.co
You're comparing pub_date, which is a datetime.date, to timezone.now(),
which is a datetime.datetime. Check the docs at
http://docs.python.org/2/library/datetime.html for more.
In order to convert timezone.now() to a date, just call
In [1]: from django.utils import timezone
In [3]: timezone.now()
Out[3]: datetime.datetime(2014, 1, 27, 14, 42, 29, 408491, tzinfo=<UTC>)
In [4]: timezone.now().date()
Out[4]: datetime.date(2014, 1, 27)
Post by Rafał Szymański
Hi again
I wolud like ask another question.
If someone would be so kind and help that will be great.
I'm in the begining of django tutorial.
I use python3.3 and django1.6
can't compare datetime.datetime to datetime.date
import datetime
from django.utils import timezone
from django.db import models
question = models.CharField(max_length=200)
pub_date = models.DateField('date published')
return self.pub_date >= timezone.now() -
datetime.timedelta(days=1)
return self.question
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
return self.choice_text
Thanks in advance
Rafał
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4614f29c-5d27-4332-b648-99c610adf626%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Continue reading on narkive:
Loading...