mirror of
https://github.com/django/django.git
synced 2025-10-28 16:16:12 +00:00
Fixed #10271, #10281 -- Fixed the handling multiple inline models that share a common base class and have the link to the inline parent on the base class. Includes modifications that allow the equivalent handling for GenericFields. Thanks to Idan Gazit, Antti Kaihola (akaihola), and Alex Gaynor for their work on this patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10017 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -32,7 +32,7 @@ class BaseFormSet(StrAndUnicode):
|
||||
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
|
||||
initial=None, error_class=ErrorList):
|
||||
self.is_bound = data is not None or files is not None
|
||||
self.prefix = prefix or 'form'
|
||||
self.prefix = prefix or self.get_default_prefix()
|
||||
self.auto_id = auto_id
|
||||
self.data = data
|
||||
self.files = files
|
||||
@@ -62,7 +62,7 @@ class BaseFormSet(StrAndUnicode):
|
||||
initial = {TOTAL_FORM_COUNT: self._total_form_count,
|
||||
INITIAL_FORM_COUNT: self._initial_form_count}
|
||||
self.management_form = ManagementForm(initial=initial, auto_id=self.auto_id, prefix=self.prefix)
|
||||
|
||||
|
||||
# construct the forms in the formset
|
||||
self._construct_forms()
|
||||
|
||||
@@ -74,7 +74,7 @@ class BaseFormSet(StrAndUnicode):
|
||||
self.forms = []
|
||||
for i in xrange(self._total_form_count):
|
||||
self.forms.append(self._construct_form(i))
|
||||
|
||||
|
||||
def _construct_form(self, i, **kwargs):
|
||||
"""
|
||||
Instantiates and returns the i-th form instance in a formset.
|
||||
@@ -118,7 +118,7 @@ class BaseFormSet(StrAndUnicode):
|
||||
|
||||
def _get_deleted_forms(self):
|
||||
"""
|
||||
Returns a list of forms that have been marked for deletion. Raises an
|
||||
Returns a list of forms that have been marked for deletion. Raises an
|
||||
AttributeError if deletion is not allowed.
|
||||
"""
|
||||
if not self.is_valid() or not self.can_delete:
|
||||
@@ -176,6 +176,11 @@ class BaseFormSet(StrAndUnicode):
|
||||
return [self.forms[i[0]] for i in self._ordering]
|
||||
ordered_forms = property(_get_ordered_forms)
|
||||
|
||||
#@classmethod
|
||||
def get_default_prefix(cls):
|
||||
return 'form'
|
||||
get_default_prefix = classmethod(get_default_prefix)
|
||||
|
||||
def non_form_errors(self):
|
||||
"""
|
||||
Returns an ErrorList of errors that aren't associated with a particular
|
||||
|
||||
@@ -249,8 +249,8 @@ class BaseModelForm(BaseForm):
|
||||
# This is an extra field that's not on the ModelForm, ignore it
|
||||
continue
|
||||
if not isinstance(f, ModelField):
|
||||
# This is an extra field that happens to have a name that matches,
|
||||
# for example, a related object accessor for this model. So
|
||||
# This is an extra field that happens to have a name that matches,
|
||||
# for example, a related object accessor for this model. So
|
||||
# get_field_by_name found it, but it is not a Field so do not proceed
|
||||
# to use it as if it were.
|
||||
continue
|
||||
@@ -472,7 +472,7 @@ class BaseInlineFormSet(BaseModelFormSet):
|
||||
# is there a better way to get the object descriptor?
|
||||
self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
|
||||
qs = self.model._default_manager.filter(**{self.fk.name: self.instance})
|
||||
super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix or self.rel_name,
|
||||
super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix,
|
||||
queryset=qs)
|
||||
|
||||
def _construct_forms(self):
|
||||
@@ -489,6 +489,12 @@ class BaseInlineFormSet(BaseModelFormSet):
|
||||
form.data[form.add_prefix(self._pk_field.name)] = None
|
||||
return form
|
||||
|
||||
#@classmethod
|
||||
def get_default_prefix(cls):
|
||||
from django.db.models.fields.related import RelatedObject
|
||||
return RelatedObject(cls.fk.rel.to, cls.model, cls.fk).get_accessor_name()
|
||||
get_default_prefix = classmethod(get_default_prefix)
|
||||
|
||||
def save_new(self, form, commit=True):
|
||||
fk_attname = self.fk.get_attname()
|
||||
kwargs = {fk_attname: self.instance.pk}
|
||||
|
||||
Reference in New Issue
Block a user