mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
[1.1.X] Restored pre-r10062 behavior allowing None from formfield_callback to exclude itself from the form
Backported from r12891 from trunk git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12892 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
2613872969
commit
64e4ffc932
@ -154,6 +154,7 @@ def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda
|
|||||||
in the ``fields`` argument.
|
in the ``fields`` argument.
|
||||||
"""
|
"""
|
||||||
field_list = []
|
field_list = []
|
||||||
|
ignored = []
|
||||||
opts = model._meta
|
opts = model._meta
|
||||||
for f in opts.fields + opts.many_to_many:
|
for f in opts.fields + opts.many_to_many:
|
||||||
if not f.editable:
|
if not f.editable:
|
||||||
@ -165,9 +166,14 @@ def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda
|
|||||||
formfield = formfield_callback(f)
|
formfield = formfield_callback(f)
|
||||||
if formfield:
|
if formfield:
|
||||||
field_list.append((f.name, formfield))
|
field_list.append((f.name, formfield))
|
||||||
|
else:
|
||||||
|
ignored.append(f.name)
|
||||||
field_dict = SortedDict(field_list)
|
field_dict = SortedDict(field_list)
|
||||||
if fields:
|
if fields:
|
||||||
field_dict = SortedDict([(f, field_dict.get(f)) for f in fields if (not exclude) or (exclude and f not in exclude)])
|
field_dict = SortedDict(
|
||||||
|
[(f, field_dict.get(f)) for f in fields
|
||||||
|
if ((not exclude) or (exclude and f not in exclude)) and (f not in ignored)]
|
||||||
|
)
|
||||||
return field_dict
|
return field_dict
|
||||||
|
|
||||||
class ModelFormOptions(object):
|
class ModelFormOptions(object):
|
||||||
|
@ -205,6 +205,22 @@ class Post(models.Model):
|
|||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
class MarkupField(models.CharField):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
kwargs["max_length"] = 20
|
||||||
|
super(MarkupField, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def formfield(self, **kwargs):
|
||||||
|
# don't allow this field to be used in form (real use-case might be
|
||||||
|
# that you know the markup will always be X, but it is among an app
|
||||||
|
# that allows the user to say it could be something else)
|
||||||
|
# regressed at r10062
|
||||||
|
return None
|
||||||
|
|
||||||
|
class CustomFieldForExclusionModel(models.Model):
|
||||||
|
name = models.CharField(max_length=10)
|
||||||
|
markup = MarkupField()
|
||||||
|
|
||||||
__test__ = {'API_TESTS': """
|
__test__ = {'API_TESTS': """
|
||||||
>>> from django import forms
|
>>> from django import forms
|
||||||
>>> from django.forms.models import ModelForm, model_to_dict
|
>>> from django.forms.models import ModelForm, model_to_dict
|
||||||
@ -1547,6 +1563,19 @@ False
|
|||||||
>>> f.is_valid()
|
>>> f.is_valid()
|
||||||
True
|
True
|
||||||
|
|
||||||
|
# Model field that returns None to exclude itself with explicit fields ########
|
||||||
|
|
||||||
|
>>> class CustomFieldForExclusionForm(ModelForm):
|
||||||
|
... class Meta:
|
||||||
|
... model = CustomFieldForExclusionModel
|
||||||
|
... fields = ['name', 'markup']
|
||||||
|
|
||||||
|
>>> CustomFieldForExclusionForm.base_fields.keys()
|
||||||
|
['name']
|
||||||
|
|
||||||
|
>>> print CustomFieldForExclusionForm()
|
||||||
|
<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="10" /></td></tr>
|
||||||
|
|
||||||
# Clean up
|
# Clean up
|
||||||
>>> import shutil
|
>>> import shutil
|
||||||
>>> shutil.rmtree(temp_storage_dir)
|
>>> shutil.rmtree(temp_storage_dir)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user