Changeset 6301
- Timestamp:
- 09/15/07 13:13:50 (1 year ago)
- Files:
-
- django/branches/newforms-admin/django/newforms/models.py (modified) (8 diffs)
- django/branches/newforms-admin/tests/regressiontests/inline_formsets (added)
- django/branches/newforms-admin/tests/regressiontests/inline_formsets/__init__.py (added)
- django/branches/newforms-admin/tests/regressiontests/inline_formsets/models.py (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/newforms-admin/django/newforms/models.py
r6030 r6301 37 37 if fields and f.name not in fields: 38 38 continue 39 f.save_form_data(instance, cleaned_data[f.name]) 39 f.save_form_data(instance, cleaned_data[f.name]) 40 40 # Wrap up the saving of m2m data as a function 41 41 def save_m2m(): … … 52 52 save_m2m() 53 53 else: 54 # We're not committing. Add a method to the form to allow deferred 54 # We're not committing. Add a method to the form to allow deferred 55 55 # saving of m2m data 56 56 form.save_m2m = save_m2m … … 62 62 return save_instance(self, model(), fields, fail_message, commit) 63 63 return save 64 64 65 65 def make_instance_save(instance, fields, fail_message): 66 66 "Returns the save() method for a Form." … … 90 90 field_list.append((f.name, formfield)) 91 91 base_fields = SortedDictFromList(field_list) 92 return type(opts.object_name + 'Form', (form,), 92 return type(opts.object_name + 'Form', (form,), 93 93 {'base_fields': base_fields, '_model': model, 'save': make_model_save(model, fields, 'created')}) 94 94 … … 212 212 def initial_data(instance, fields=None): 213 213 """ 214 Return a dictionary from data in ``instance`` that is suitable for 214 Return a dictionary from data in ``instance`` that is suitable for 215 215 use as a ``Form`` constructor's ``initial`` argument. 216 216 217 217 Provide ``fields`` to specify the names of specific fields to return. 218 218 All field values in the instance will be returned if ``fields`` is not … … 235 235 """ 236 236 model = None 237 237 238 238 def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, instances=None): 239 239 self.instances = instances … … 309 309 """ 310 310 Returns an ``InlineFormset`` for the given kwargs. 311 311 312 312 You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey`` 313 313 to ``parent_model``. … … 324 324 else: 325 325 raise Exception("%s has more than 1 ForeignKey to %s" % (model, parent_model)) 326 else: 327 fks_to_parent = [f for f in opts.fields if f.name == fk_name] 328 if len(fks_to_parent) == 1: 329 fk = fks_to_parent[0] 330 if not isinstance(fk, ForeignKey) or fk.rel.to != parent_model: 331 raise Exception("fk_name '%s' is not a ForeignKey to %s" % (fk_name, parent_model)) 332 elif len(fks_to_parent) == 0: 333 raise Exception("%s has no field named '%s'" % (model, fk_name)) 326 334 # let the formset handle object deletion by default 327 FormSet = formset_for_model(model, formset=InlineFormset, fields=fields, 328 formfield_callback=formfield_callback, 329 extra=extra, orderable=orderable, 335 FormSet = formset_for_model(model, formset=InlineFormset, fields=fields, 336 formfield_callback=formfield_callback, 337 extra=extra, orderable=orderable, 330 338 deletable=deletable) 331 339 # HACK: remove the ForeignKey to the parent from every form
