开发者

django db lookup for datetime

I'm trying to filter database entries by their datetime:

models.py:

from django.db import models


class ContestEvent(models.Model):
    year = models.DateTimeField()
    month = models.DateTimeField()

In my view I define a function calender:

def calendar(request, pYear, pMonth):
    """
    Show calendar of events for specified month and year
    """
    lYear = int(pYear)
    lMonth = int(pMonth)
    lCalendarFromMonth = datetime(lYear, lMonth, 1)
    lCalendarToMonth = datetime(lYear, lMonth, monthrange(lYear, lMonth)[1])
    my_workouts = ContestEvent.objects.filter(id__year=lCalendarFromMonth, id__month=lCalendarToMonth)
    lCalendar = html_calendar.WorkoutCalendar开发者_如何学C(my_workouts).formatmonth(lYear, lMonth)

Then I got the following error:

Request Method:   GET
Request URL:  http://127.0.0.1:8000/htmlcalendar/
Exception Type:  TypeError
Exception Value:  

int() argument must be a string or a number, not 'datetime.datetime'

Exception Location:  C:\Python25\lib\site-packages\django\db\models\fields\__init__.py in get_db_prep_lookup, line 225

The error is the same, when I define my Model with year = models.IntegerField() instead of year = models.DateTimeField()

What is wrong here. I'm a Django Beginner. Thank you in advance !!


I would say if you want the month and year then use IntegerField, and when you do that you no longer need to create datetime objects to pass to your filter. Try something like this:

class ContestEvent(models.Model):
    year = models.IntegerField()
    month = models.IntegerField()

def calendar(request, pYear, pMonth):
    """
    Show calendar of events for specified month and year
    """
    lYear = int(pYear)
    lMonth = int(pMonth)
    my_workouts = ContestEvent.objects.filter(year=lYear, month=lMonth)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜