Filtering by month doesn't work

63 views
Skip to first unread message

Vojtěch Tranta

unread,
Nov 25, 2013, 6:24:21 PM11/25/13
to django...@googlegroups.com
Hi,
do you have any clue why this does not work?

def index(request):
if request.method == 'POST' and request.POST:
event_form = EventForm(request.POST)
event = event_form.save()

today = datetime.date.today();
year = int(today.strftime('%Y'))
month = int(today.strftime('%m'))
events = Event.objects.filter(end__month='11')
# events = Event.objects.all()
pdb.set_trace()
cal = jvccalendar.HTMLCalendar(jvccalendar.MONDAY).formatmonth(year, month, events)
form = EventForm()
return render_to_response('jvc/index.html', {'cal': cal, 'side_form': form}, context_instance=RequestContext(request))

I am still getting "events" as empty, filtering using filter(end_start='2013') works, but filtering by month does not, I googled as much as I could, but no luck. I copied code from StackOverflow, but no luck.... 
Thank you!

Начаров Михаил

unread,
Nov 25, 2013, 9:54:09 PM11/25/13
to django...@googlegroups.com
Hi Tranta.
In my projects this functionality works fine. 
What version of django did you used? 
Do you sure that field end in Event table contains November dates?

Also, you can use today.month instead of  int(today.strftime('%m')) and today.year instead of  int(today.strftime('%Y')).
And in django docs says that it necessary to validate your form before use it:
if event_form.is_valid():
    event = event_form.save()

вторник, 26 ноября 2013 г., 5:24:21 UTC+6 пользователь Vojtěch Tranta написал:

Leonardo Giordani

unread,
Nov 26, 2013, 2:25:40 AM11/26/13
to django...@googlegroups.com
May you please post your Event model?

Leonardo Giordani
Author of The Digital Cat
My profile on About.me - My GitHub page - My Coderwall profile


2013/11/26 Начаров Михаил <michael....@gmail.com>

--
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/4521ed83-d85b-42a5-a344-27ac273648b9%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Vojtěch Tranta

unread,
Nov 26, 2013, 12:13:06 PM11/26/13
to django...@googlegroups.com
I am no using that condition,
I am absolutely sure that I have november dates in my DB (i am using MySQL).
I tried everything in terms of using datetime to compare db month with, no luck so far....

Dne úterý, 26. listopadu 2013 3:54:09 UTC+1 Начаров Михаил napsal(a):

Vojtěch Tranta

unread,
Nov 26, 2013, 12:13:41 PM11/26/13
to django...@googlegroups.com
from django.db import models
from django.contrib.auth.models import User
from student import Student
from eventtype import EventType
from django import forms
import datetime

class Event(models.Model):
user = models.ForeignKey(User)
student = models.ForeignKey(Student)
place = models.CharField(max_length=200, null=True)
type = models.ForeignKey(EventType)
start = models.DateTimeField('event start', default=datetime.datetime.now)
end = models.DateTimeField('event end', default=datetime.datetime.now)
absence = models.NullBooleanField()
note = models.TextField(null=True)
created = models.DateTimeField('created time')
created_by = models.ForeignKey(User, related_name='event creator')
description = models.TextField(null=True)

class Meta:
app_label = 'jvc'

def __unicode__(self):
return self.type.name +' '+ self.student.fullname +' '+self.start.strftime('%d.%m.%Y')

I should mention that I have my models in separated files in "models" folder inside app called "jvc"

Dne úterý, 26. listopadu 2013 8:25:40 UTC+1 Leo napsal(a):

Leonardo Giordani

unread,
Nov 26, 2013, 12:30:24 PM11/26/13
to django...@googlegroups.com
Are you sure that filtering using a string is the same as filtering using an int? I think that Django doesn't automatically convert it. So the condition


events = Event.objects.filter(end__month='11')

should be

events = Event.objects.filter(end__month=11)

Indeed documentation does things this way. I see that you succeded in filtering with

filter(end_start='2013')

perhaps end__startswith? In that case you are treating it as a string.
May you please check this and let me know?

Cheers,

Leo



Leonardo Giordani
Author of The Digital Cat
My profile on About.me - My GitHub page - My Coderwall profile


2013/11/26 Vojtěch Tranta <vojta....@gmail.com>

Joseph Mutumi

unread,
Nov 29, 2013, 9:17:33 AM11/29/13
to django...@googlegroups.com
I think the model is not being saved because no instance is being constructed by the form. Either make call to event_form.is_valid() of event_form.full_clean(), the former being preferred.


Reply all
Reply to author
Forward
0 new messages