views:

599

answers:

4

Hello,

year_id (2004 etc) is a dropdown box on the template. The users selects a year and this is retrieved as year_id in the view. I need to select publications based on year from a field called grantstartdt which is in the format 2008-01-01. The error is:

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

year_id = request.GET['year_id']  
yr = get_object_or_404(Year, pk=year_id) 
projectyr=Researchproject.objects.exclude(activeyesno = 2).filter(grantstartdt__year=yr)

Thank you for any assistance!

A: 

Try passing year_id instead of yr in that filter() call:

projectyr=Researchproject.objects.exclude(activeyesno = 2).filter(grantstartdt__year=year_id)
Steef
A: 

I still received an error. I need to somehow parse the year for the grantstartdt format 2008-01-01 so it is 2008 and then it needs to match the value of the yr, which should also be 2008, 2007, etc.

A: 

Thats because you are referring to the year object instead of the actual year. Assuming your Year model has a integer field called xyz. Your code should look like..

year_id = request.GET['year_id']  
yr = get_object_or_404(Year, pk=year_id) 
projectyr=Researchproject.objects.exclude(activeyesno = 2).filter(grantstartdt__year=yr.xyz)
Chirayu Patel
A: 

That would work but I need to parse the grantstartdt. It is in a date format YYYY-MM-DD. I'm trying to substring out the YYYY part and match it to the value in the Year field. There is only the id and the year values in Year and year is YYYY. How do I substring grantstartdt and then link it to Year?

When I did the xyz code this is the error:

Enter a valid date in YYYY-MM-DD format.