Ticket #5488: newforms_admin_inline.patch
File newforms_admin_inline.patch, 4.1 KB (added by , 17 years ago) |
---|
-
django/newforms/models.py
36 36 continue 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(): 42 42 opts = instance.__class__._meta … … 51 51 instance.save() 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 57 57 return instance … … 61 61 def save(self, commit=True): 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." 67 67 def save(self, commit=True): … … 89 89 if formfield: 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 95 95 def form_for_instance(instance, form=BaseForm, fields=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): … … 211 211 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 219 219 provided. … … 234 234 A ``FormSet`` attatched to a particular model or sequence of model instances. 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 240 240 kwargs = {'data': data, 'files': files, 'auto_id': auto_id, 'prefix': prefix} … … 308 308 def inline_formset(parent_model, model, fk_name=None, fields=None, extra=3, orderable=False, deletable=True, formfield_callback=lambda f: f.formfield()): 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``. 314 314 """ … … 323 323 raise Exception("%s has no ForeignKey to %s" % (model, 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 ForeignKey %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 332 340 # This should be done a line above before we pass 'fields' to formset_for_model