Django

Code

Changeset 6301

Show
Ignore:
Timestamp:
09/15/07 13:13:50 (1 year ago)
Author:
jkocherhans
Message:

newforms-admin: Fixed #5488. inlines with multiple ForeignKeys? to the same parent don't work. Thanks jdetaeye. Also, some whitespace cleanup. Sorry for the mess.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin/django/newforms/models.py

    r6030 r6301  
    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(): 
     
    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 
     
    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." 
     
    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 
     
    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 
     
    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 
     
    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``. 
     
    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 field named '%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