Ticket #5488: newforms_admin_inline.patch

File newforms_admin_inline.patch, 4.1 KB (added by jdetaeye, 17 years ago)

implementation patch

  • django/newforms/models.py

     
    3636            continue
    3737        if fields and f.name not in fields:
    3838            continue
    39         f.save_form_data(instance, cleaned_data[f.name])       
     39        f.save_form_data(instance, cleaned_data[f.name])
    4040    # Wrap up the saving of m2m data as a function
    4141    def save_m2m():
    4242        opts = instance.__class__._meta
     
    5151        instance.save()
    5252        save_m2m()
    5353    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
    5555        # saving of m2m data
    5656        form.save_m2m = save_m2m
    5757    return instance
     
    6161    def save(self, commit=True):
    6262        return save_instance(self, model(), fields, fail_message, commit)
    6363    return save
    64    
     64
    6565def make_instance_save(instance, fields, fail_message):
    6666    "Returns the save() method for a Form."
    6767    def save(self, commit=True):
     
    8989        if formfield:
    9090            field_list.append((f.name, formfield))
    9191    base_fields = SortedDictFromList(field_list)
    92     return type(opts.object_name + 'Form', (form,), 
     92    return type(opts.object_name + 'Form', (form,),
    9393        {'base_fields': base_fields, '_model': model, 'save': make_model_save(model, fields, 'created')})
    9494
    9595def form_for_instance(instance, form=BaseForm, fields=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
     
    211211
    212212def initial_data(instance, fields=None):
    213213    """
    214     Return a dictionary from data in ``instance`` that is suitable for 
     214    Return a dictionary from data in ``instance`` that is suitable for
    215215    use as a ``Form`` constructor's ``initial`` argument.
    216    
     216
    217217    Provide ``fields`` to specify the names of specific fields to return.
    218218    All field values in the instance will be returned if ``fields`` is not
    219219    provided.
     
    234234    A ``FormSet`` attatched to a particular model or sequence of model instances.
    235235    """
    236236    model = None
    237    
     237
    238238    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, instances=None):
    239239        self.instances = instances
    240240        kwargs = {'data': data, 'files': files, 'auto_id': auto_id, 'prefix': prefix}
     
    308308def inline_formset(parent_model, model, fk_name=None, fields=None, extra=3, orderable=False, deletable=True, formfield_callback=lambda f: f.formfield()):
    309309    """
    310310    Returns an ``InlineFormset`` for the given kwargs.
    311    
     311
    312312    You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey``
    313313    to ``parent_model``.
    314314    """
     
    323323            raise Exception("%s has no ForeignKey to %s" % (model, parent_model))
    324324        else:
    325325            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))
    326334    # 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,
    330338                                deletable=deletable)
    331339    # HACK: remove the ForeignKey to the parent from every form
    332340    # This should be done a line above before we pass 'fields' to formset_for_model
Back to Top