On May 12, 6:47 am, zachp <zpice...@gmail.com> wrote: > ... I need the user who does this to be able to select a > month (and possibly a year) for the drawing she wants to run. I need > django to convert those two data points into a datetime object for me, > but it's not readily obvious how to do that.
Are you SURE you need a datetime object? I'm guessing you will actually end up needing TWO datetime objects if you continue down that line of thought (one for the beginning of the month, one for the end of the month, eh?). I solved the same problem you're describing, like so: (paraphrased and may contain typos. It's also from my Very First python/django project, so it's far from "optimal". I don't remember why I zero-padded the month :-) class ThingyForm(forms.Form): year = forms.ChoiceField(choices=[('2006',"2006"), ('2007',"2007"), ('2008',"2008"), ('2009',"2009"), ('2010',"2010"),]) month = forms.ChoiceField(choices=[('01',"Jan"), ('02',"Feb"), ('03',"Mar"), ('04',"Apr"), ('05',"May"), ('06',"Jun"), ('07',"Jul"), ('08',"Aug"), ('09',"Sep"), ('10',"Oct"), ('11',"Nov"), ('12',"Dec"), ('all',"ANNUAL")]) def thingyview(httpreq): today = datetime.now() year = str(today.year) month = ("0"+ str(today.month))[-2:] if httpreq.method=="POST": thingyform = ThingyForm(httpreq.POST) if thingyform.is_valid(): year = thingyform.cleaned_data['year'] month = thingyform.cleaned_data['month'] else: thingyform = ThingyForm(initial={'year':year, 'month':month}) # Ultimately, I needed the year and month to limit the selections I got from the database (via django's ORM), and in my case I also wanted the option to select ALL months for that year. So here's where the cool part is, where I do that without needing any datetime objects: thingylist = Thingies.objects.filter(funtime__year=year) if not month=='all': thingylist = thingylist.filter(funtime__month=month) It's so easy that it didn't even jump out at me on my first read of the docs. It's definitely easier than coming up with TWO datetime objects and filtering Thingies with a funtime between them. --Rob --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---