﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25475	Document how to to use a literal % in Func.template	Matt Cooper	nobody	"I have a database function to annotate the week of a date. It worked in 1.8 but breaks in 1.9a1, failing with a ""ValueError: unsupported format character 'W'"" exception.

{{{
class Week(Func):
    function = 'strftime'
    template = ""%(function)s('%%W',%(expressions)s)""

def data_last_n_weeks(user, week_count):
    assert week_count > 0, ""week_count must be greater than 0""
    assert week_count < 52, ""week_count must be less than 52""

    today = date.today()
    most_recent_monday = today - timedelta(days=(today.isoweekday()-1))
    start_date = most_recent_monday - timedelta(days=7*(week_count-1))

    data = user.serving_set.filter(date__range=(start_date, today)
            ).annotate(week=Week('date', output_field=IntegerField())).values(
            'week').annotate(amt=Sum('amount'), cost=Sum('cost'))

    return data
}}}

yields

{{{
  File ""/Users/[me]/Projects/[proj]/ENV/lib/python3.4/site-packages/django/db/backends/sqlite3/operations.py"", line 133, in last_executed_query
    return sql % params
ValueError: unsupported format character 'W' (0x57) at index 18
}}}"	Cleanup/optimization	closed	Documentation	1.8	Normal	fixed	func strftime	josh.smeaton@…	Ready for checkin	1	0	0	0	0	0
