Author: jkocherhans
Date: 2007-10-13 01:54:14 -0500 (Sat, 13 Oct 2007)
New Revision: 6476

Modified:
   django/branches/newforms-admin/django/newforms/models.py
Log:
newforms-admin: Extracted a method to get the ForeignKey from one model to 
another from inline_formset.


Modified: django/branches/newforms-admin/django/newforms/models.py
===================================================================
--- django/branches/newforms-admin/django/newforms/models.py    2007-10-13 
02:57:57 UTC (rev 6475)
+++ django/branches/newforms-admin/django/newforms/models.py    2007-10-13 
06:54:14 UTC (rev 6476)
@@ -311,17 +311,24 @@
         new_obj = self.model(**kwargs)
         return save_instance(form, new_obj, commit=commit)
 
-def inline_formset(parent_model, model, fk_name=None, fields=None, extra=3, 
orderable=False, deletable=True, formfield_callback=lambda f: f.formfield()):
+def get_foreign_key(parent_model, model, fk_name=None):
     """
-    Returns an ``InlineFormset`` for the given kwargs.
-
-    You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey``
-    to ``parent_model``.
+    Finds and returns the ForeignKey from model to parent if there is one.
+    If fk_name is provided, assume it is the name of the ForeignKey field.
     """
+    # avoid circular import
     from django.db.models import ForeignKey
     opts = model._meta
-    # figure out what the ForeignKey from model to parent_model is
-    if fk_name is None:
+    if fk_name:
+        fks_to_parent = [f for f in opts.fields if f.name == fk_name]
+        if len(fks_to_parent) == 1:
+            fk = fks_to_parent[0]
+            if not isinstance(fk, ForeignKey) or fk.rel.to != parent_model:
+                raise Exception("fk_name '%s' is not a ForeignKey to %s" % 
(fk_name, parent_model))
+        elif len(fks_to_parent) == 0:
+            raise Exception("%s has no field named '%s'" % (model, fk_name))
+    else:
+        # Try to discover what the ForeignKey from model to parent_model is
         fks_to_parent = [f for f in opts.fields if isinstance(f, ForeignKey) 
and f.rel.to == parent_model]
         if len(fks_to_parent) == 1:
             fk = fks_to_parent[0]
@@ -329,14 +336,16 @@
             raise Exception("%s has no ForeignKey to %s" % (model, 
parent_model))
         else:
             raise Exception("%s has more than 1 ForeignKey to %s" % (model, 
parent_model))
-    else:
-        fks_to_parent = [f for f in opts.fields if f.name == fk_name]
-        if len(fks_to_parent) == 1:
-            fk = fks_to_parent[0]
-            if not isinstance(fk, ForeignKey) or fk.rel.to != parent_model:
-                raise Exception("fk_name '%s' is not a ForeignKey to %s" % 
(fk_name, parent_model))
-        elif len(fks_to_parent) == 0:
-            raise Exception("%s has no field named '%s'" % (model, fk_name))
+    return fk
+
+def inline_formset(parent_model, model, fk_name=None, fields=None, extra=3, 
orderable=False, deletable=True, formfield_callback=lambda f: f.formfield()):
+    """
+    Returns an ``InlineFormset`` for the given kwargs.
+
+    You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey``
+    to ``parent_model``.
+    """
+    fk = get_foreign_key(parent_model, model, fk_name=fk_name)
     # let the formset handle object deletion by default
     FormSet = formset_for_model(model, formset=InlineFormset, fields=fields,
                                 formfield_callback=formfield_callback,


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to