Here is a snippet here that sets the day to 1 (suppose you have a DateField so that you get this value, you need to get some day).
The code looks like this (just in case, Django fragments disappear):
import datetime import re from django.forms.widgets import Widget, Select from django.utils.dates import MONTHS from django.utils.safestring import mark_safe __all__ = ('MonthYearWidget',) RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$') class MonthYearWidget(Widget): """ A Widget that splits date input into two <select> boxes for month and year, with 'day' defaulting to the first of the month. Based on SelectDateWidget, in django/trunk/django/forms/extras/widgets.py """ none_value = (0, '---') month_field = '%s_month' year_field = '%s_year' def __init__(self, attrs=None, years=None, required=True):
Dominic Rodger
source share