Tuesday, January 28, 2014

Re: can't compare datetime.datetime to datetime.date

Thank You so much.
Best wishes
Rafal

W dniu poniedziałek, 27 stycznia 2014 15:43:59 UTC+1 użytkownik i...@simpler.co napisał:
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)


On Monday, January 27, 2014 5:38:20 AM UTC-8, Rafał Szymański wrote:
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

No comments:

Post a Comment