Friday, November 29, 2013

Re: Filtering by month doesn't work

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.


On Tue, Nov 26, 2013 at 8:30 PM, Leonardo Giordani <giordani.leonardo@gmail.com> wrote:
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.tranta@gmail.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):
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>
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 написал:
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!

--
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.

--
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/5a50a517-fe4f-48b5-a713-c5042b5cab3b%40googlegroups.com.

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

--
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/CAEhE%2BOm2eMP0YAgQEbUQj7rtAMjrQWV7nigUnbciBq1FfzfecA%40mail.gmail.com.

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

--
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/CAN5idp_uPS8R9tRWu1RJzjthxx39UAxZK-trET6dYVghE8M-rA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment