Django

Code

Changeset 6476

Show
Ignore:
Timestamp:
10/13/07 01:54:14 (1 year ago)
Author:
jkocherhans
Message:

newforms-admin: Extracted a method to get the ForeignKey? from one model to another from inline_formset.

Files:

Legend:

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

    r6418 r6476  
    312312        return save_instance(form, new_obj, commit=commit) 
    313313 
    314 def inline_formset(parent_model, model, fk_name=None, fields=None, extra=3, orderable=False, deletable=True, formfield_callback=lambda f: f.formfield()): 
    315     """ 
    316     Returns an ``InlineFormset`` for the given kwargs. 
    317  
    318     You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey`` 
    319     to ``parent_model``. 
    320     """ 
     314def get_foreign_key(parent_model, model, fk_name=None): 
     315    """ 
     316    Finds and returns the ForeignKey from model to parent if there is one. 
     317    If fk_name is provided, assume it is the name of the ForeignKey field. 
     318    """ 
     319    # avoid circular import 
    321320    from django.db.models import ForeignKey 
    322321    opts = model._meta 
    323     # figure out what the ForeignKey from model to parent_model is 
    324     if fk_name is None: 
     322    if fk_name: 
     323        fks_to_parent = [f for f in opts.fields if f.name == fk_name] 
     324        if len(fks_to_parent) == 1: 
     325            fk = fks_to_parent[0] 
     326            if not isinstance(fk, ForeignKey) or fk.rel.to != parent_model: 
     327                raise Exception("fk_name '%s' is not a ForeignKey to %s" % (fk_name, parent_model)) 
     328        elif len(fks_to_parent) == 0: 
     329            raise Exception("%s has no field named '%s'" % (model, fk_name)) 
     330    else: 
     331        # Try to discover what the ForeignKey from model to parent_model is 
    325332        fks_to_parent = [f for f in opts.fields if isinstance(f, ForeignKey) and f.rel.to == parent_model] 
    326333        if len(fks_to_parent) == 1: 
     
    330337        else: 
    331338            raise Exception("%s has more than 1 ForeignKey to %s" % (model, parent_model)) 
    332     else: 
    333         fks_to_parent = [f for f in opts.fields if f.name == fk_name] 
    334         if len(fks_to_parent) == 1: 
    335             fk = fks_to_parent[0] 
    336             if not isinstance(fk, ForeignKey) or fk.rel.to != parent_model: 
    337                 raise Exception("fk_name '%s' is not a ForeignKey to %s" % (fk_name, parent_model)) 
    338         elif len(fks_to_parent) == 0: 
    339             raise Exception("%s has no field named '%s'" % (model, fk_name)) 
     339    return fk 
     340 
     341def inline_formset(parent_model, model, fk_name=None, fields=None, extra=3, orderable=False, deletable=True, formfield_callback=lambda f: f.formfield()): 
     342    """ 
     343    Returns an ``InlineFormset`` for the given kwargs. 
     344 
     345    You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey`` 
     346    to ``parent_model``. 
     347    """ 
     348    fk = get_foreign_key(parent_model, model, fk_name=fk_name) 
    340349    # let the formset handle object deletion by default 
    341350    FormSet = formset_for_model(model, formset=InlineFormset, fields=fields,