mirror of
https://github.com/django/django.git
synced 2025-07-05 02:09:13 +00:00
newforms-admin: Fixed #5828. You can leave inline add forms empty again. Thanks brosner.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@6837 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
e4910ce5b7
commit
b15703d322
@ -182,12 +182,11 @@ class BaseForm(StrAndUnicode):
|
|||||||
for name, field in self.fields.items():
|
for name, field in self.fields.items():
|
||||||
if name in exceptions:
|
if name in exceptions:
|
||||||
continue
|
continue
|
||||||
# value_from_datadict() gets the data from the dictionary.
|
# value_from_datadict() gets the data from the data dictionaries.
|
||||||
# Each widget type knows how to retrieve its own data, because some
|
# Each widget type knows how to retrieve its own data, because some
|
||||||
# widgets split data over several HTML fields.
|
# widgets split data over several HTML fields.
|
||||||
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
|
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
|
||||||
# HACK: ['', ''] and [None, None] deal with SplitDateTimeWidget. This should be more robust.
|
if not field.widget.is_empty(value):
|
||||||
if value not in (None, '', ['', ''], [None, None]):
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -166,6 +166,15 @@ class Widget(object):
|
|||||||
"""
|
"""
|
||||||
return data.get(name, None)
|
return data.get(name, None)
|
||||||
|
|
||||||
|
def is_empty(self, value):
|
||||||
|
"""
|
||||||
|
Given a dictionary of data and this widget's name, return True if the
|
||||||
|
widget data is empty or False when not empty.
|
||||||
|
"""
|
||||||
|
if value not in (None, ''):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def id_for_label(self, id_):
|
def id_for_label(self, id_):
|
||||||
"""
|
"""
|
||||||
Returns the HTML ID attribute of this Widget for use by a <label>,
|
Returns the HTML ID attribute of this Widget for use by a <label>,
|
||||||
@ -302,6 +311,11 @@ class CheckboxInput(Widget):
|
|||||||
return False
|
return False
|
||||||
return super(CheckboxInput, self).value_from_datadict(data, files, name)
|
return super(CheckboxInput, self).value_from_datadict(data, files, name)
|
||||||
|
|
||||||
|
def is_empty(self, value):
|
||||||
|
# this widget will always either be True or False, so always return the
|
||||||
|
# opposite value so False values will make the form empty
|
||||||
|
return not value
|
||||||
|
|
||||||
class Select(Widget):
|
class Select(Widget):
|
||||||
def __init__(self, attrs=None, choices=()):
|
def __init__(self, attrs=None, choices=()):
|
||||||
super(Select, self).__init__(attrs)
|
super(Select, self).__init__(attrs)
|
||||||
@ -344,6 +358,12 @@ class NullBooleanSelect(Select):
|
|||||||
value = data.get(name, None)
|
value = data.get(name, None)
|
||||||
return {u'2': True, u'3': False, True: True, False: False}.get(value, None)
|
return {u'2': True, u'3': False, True: True, False: False}.get(value, None)
|
||||||
|
|
||||||
|
def is_empty(self, value):
|
||||||
|
# this widget will always either be True, False or None, so always
|
||||||
|
# return the opposite value so False and None values will make the
|
||||||
|
# form empty.
|
||||||
|
return not value
|
||||||
|
|
||||||
class SelectMultiple(Widget):
|
class SelectMultiple(Widget):
|
||||||
def __init__(self, attrs=None, choices=()):
|
def __init__(self, attrs=None, choices=()):
|
||||||
super(SelectMultiple, self).__init__(attrs)
|
super(SelectMultiple, self).__init__(attrs)
|
||||||
@ -540,6 +560,12 @@ class MultiWidget(Widget):
|
|||||||
def value_from_datadict(self, data, files, name):
|
def value_from_datadict(self, data, files, name):
|
||||||
return [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)]
|
return [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)]
|
||||||
|
|
||||||
|
def is_empty(self, value):
|
||||||
|
for widget, val in zip(self.widgets, value):
|
||||||
|
if not widget.is_empty(val):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def format_output(self, rendered_widgets):
|
def format_output(self, rendered_widgets):
|
||||||
"""
|
"""
|
||||||
Given a list of rendered widgets (as strings), returns a Unicode string
|
Given a list of rendered widgets (as strings), returns a Unicode string
|
||||||
|
@ -292,6 +292,12 @@ checkboxes).
|
|||||||
>>> w.value_from_datadict({}, {}, 'testing')
|
>>> w.value_from_datadict({}, {}, 'testing')
|
||||||
False
|
False
|
||||||
|
|
||||||
|
The CheckboxInput widget will always be empty when there is a False value
|
||||||
|
>>> w.is_empty(False)
|
||||||
|
True
|
||||||
|
>>> w.is_empty(True)
|
||||||
|
False
|
||||||
|
|
||||||
# Select Widget ###############################################################
|
# Select Widget ###############################################################
|
||||||
|
|
||||||
>>> w = Select()
|
>>> w = Select()
|
||||||
@ -453,6 +459,15 @@ over multiple times without getting consumed:
|
|||||||
<option value="3" selected="selected">No</option>
|
<option value="3" selected="selected">No</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
The NullBooleanSelect widget will always be empty when Unknown or No is selected
|
||||||
|
as its value. This is to stay compliant with the CheckboxInput behavior
|
||||||
|
>>> w.is_empty(False)
|
||||||
|
True
|
||||||
|
>>> w.is_empty(None)
|
||||||
|
True
|
||||||
|
>>> w.is_empty(True)
|
||||||
|
False
|
||||||
|
|
||||||
""" + \
|
""" + \
|
||||||
r""" # [This concatenation is to keep the string below the jython's 32K limit].
|
r""" # [This concatenation is to keep the string below the jython's 32K limit].
|
||||||
# SelectMultiple Widget #######################################################
|
# SelectMultiple Widget #######################################################
|
||||||
@ -895,6 +910,16 @@ u'<input id="foo_0" type="text" class="big" value="john" name="name_0" /><br /><
|
|||||||
>>> w.render('name', ['john', 'lennon'])
|
>>> w.render('name', ['john', 'lennon'])
|
||||||
u'<input id="bar_0" type="text" class="big" value="john" name="name_0" /><br /><input id="bar_1" type="text" class="small" value="lennon" name="name_1" />'
|
u'<input id="bar_0" type="text" class="big" value="john" name="name_0" /><br /><input id="bar_1" type="text" class="small" value="lennon" name="name_1" />'
|
||||||
|
|
||||||
|
The MultiWidget will be empty only when all widgets are considered empty.
|
||||||
|
>>> w.is_empty(['john', 'lennon'])
|
||||||
|
False
|
||||||
|
>>> w.is_empty(['john', ''])
|
||||||
|
False
|
||||||
|
>>> w.is_empty(['', ''])
|
||||||
|
True
|
||||||
|
>>> w.is_empty([None, None])
|
||||||
|
True
|
||||||
|
|
||||||
# SplitDateTimeWidget #########################################################
|
# SplitDateTimeWidget #########################################################
|
||||||
|
|
||||||
>>> w = SplitDateTimeWidget()
|
>>> w = SplitDateTimeWidget()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user