modelform represents the 'extra' item in an inlineformset The
has_changed method always returns true on post even if the 'extra'
item was not changed. In the debugger the changed_data attribute shows
the fields with the custom widget. Comparing the values before and
after the post show that they are identical and have in fact not
changed. This causes a failure in validation when existing items in
the formset are changed and a new item should not be added. When using
the default widget for time entry the the behavior is exactly as I
would expect it (the extra item does not appear changed unless i
change it). Is there a method in the widget or the field I need to
overload to make this properly handle the change value comparison when
a post occurrs?
from my forms.py:
class TimeSelectField(MultiValueField):
def __init__(self, *args, **kwargs):
fields = (
forms.ChoiceField(choices=HOUR_CHOICES),
forms.ChoiceField(choices=MINUTE_CHOICES)
)
super(TimeSelectField, self).__init__(fields, *args,
**kwargs)
def compress(self, data_list):
if data_list is not None:
return time(hour=int(data_list[0]),
minute=int(data_list[1]))
return time(hour=0, minute=0)
class TimeSelectWidget(MultiWidget):
def __init__(self, *args, **kwargs):
widgets = (
forms.Select(choices=HOUR_CHOICES),
forms.Select(choices=MINUTE_CHOICES)
)
super(TimeSelectWidget, self).__init__(widgets, *args,
**kwargs)
def decompress(self, value):
if value is not None:
return [str(value.hour).zfill(2),
str(value.minute).zfill(2)]
return ['00', '00']
def format_output(self, rendered_widgets):
return u'\n'.join(rendered_widgets)
class ScheduleEntryForm(forms.ModelForm):
start_time = TimeSelectField(widget=TimeSelectWidget)
end_time = TimeSelectField(widget=TimeSelectWidget)
class Meta:
model = ScheduleEntry
ScheduleEntryFormset = inlineformset_factory(Schedule, ScheduleEntry,
can_delete=True, extra=1, form=ScheduleEntryForm )
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment