Ticket #13633: models.py-diffs

File models.py-diffs, 2.7 KB (added by kent@…, 14 years ago)

default formfield_callback in a single place

Line 
1Index: models.py
2===================================================================
3--- models.py (revision 13431)
4+++ models.py (working copy)
5@@ -150,7 +150,7 @@
6 data[f.name] = f.value_from_object(instance)
7 return data
8
9-def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
10+def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_callback=None):
11 """
12 Returns a ``SortedDict`` containing form fields for the given model.
13
14@@ -175,6 +175,10 @@
15 kwargs = {'widget': widgets[f.name]}
16 else:
17 kwargs = {}
18+
19+ if not formfield_callback:
20+ formfield_callback = lambda f, **kwargs: f.formfield(**kwargs)
21+
22 formfield = formfield_callback(f, **kwargs)
23 if formfield:
24 field_list.append((f.name, formfield))
25@@ -198,8 +202,8 @@
26
27 class ModelFormMetaclass(type):
28 def __new__(cls, name, bases, attrs):
29- formfield_callback = attrs.pop('formfield_callback',
30- lambda f, **kwargs: f.formfield(**kwargs))
31+ formfield_callback = attrs.pop('formfield_callback', None)
32+
33 try:
34 parents = [b for b in bases if issubclass(b, ModelForm)]
35 except NameError:
36@@ -375,8 +379,7 @@
37 class ModelForm(BaseModelForm):
38 __metaclass__ = ModelFormMetaclass
39
40-def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
41- formfield_callback=lambda f: f.formfield()):
42+def modelform_factory(model, form=ModelForm, fields=None, exclude=None, formfield_callback=None):
43 # Create the inner Meta class. FIXME: ideally, we should be able to
44 # construct a ModelForm without creating and passing in a temporary
45 # inner class.
46@@ -658,7 +661,7 @@
47 form.fields[self._pk_field.name] = ModelChoiceField(qs, initial=pk_value, required=False, widget=HiddenInput)
48 super(BaseModelFormSet, self).add_fields(form, index)
49
50-def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.formfield(),
51+def modelformset_factory(model, form=ModelForm, formfield_callback=None,
52 formset=BaseModelFormSet,
53 extra=1, can_delete=False, can_order=False,
54 max_num=None, fields=None, exclude=None):
55@@ -813,7 +816,7 @@
56 formset=BaseInlineFormSet, fk_name=None,
57 fields=None, exclude=None,
58 extra=3, can_order=False, can_delete=True, max_num=None,
59- formfield_callback=lambda f: f.formfield()):
60+ formfield_callback=None):
61 """
62 Returns an ``InlineFormSet`` for the given kwargs.
63
Back to Top