Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms-admin: Merged to [4387]

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4388 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c93d1932fec26fb4f13ee0259628307f46fff41d 1 parent 259f083
Adrian Holovaty authored January 22, 2007

Showing 1 changed file with 13 additions and 4 deletions. Show diff stats Hide diff stats

  1. 17  django/newforms/models.py
17  django/newforms/models.py
@@ -47,33 +47,42 @@ def save(self, commit=True):
47 47
         return save_instance(self, instance, commit)
48 48
     return save
49 49
 
50  
-def form_for_model(model, form=BaseForm):
  50
+def form_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfield()):
51 51
     """
52 52
     Returns a Form class for the given Django model class.
53 53
 
54 54
     Provide ``form`` if you want to use a custom BaseForm subclass.
  55
+
  56
+    Provide ``formfield_callback`` if you want to define different logic for
  57
+    determining the formfield for a given database field. It's a callable that
  58
+    takes a database Field instance and returns a form Field instance.
55 59
     """
56 60
     opts = model._meta
57 61
     field_list = []
58 62
     for f in opts.fields + opts.many_to_many:
59  
-        formfield = f.formfield()
  63
+        formfield = formfield_callback(f)
60 64
         if formfield:
61 65
             field_list.append((f.name, formfield))
62 66
     fields = SortedDictFromList(field_list)
63 67
     return type(opts.object_name + 'Form', (form,), {'fields': fields, '_model': model, 'save': model_save})
64 68
 
65  
-def form_for_instance(instance, form=BaseForm):
  69
+def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
66 70
     """
67 71
     Returns a Form class for the given Django model instance.
68 72
 
69 73
     Provide ``form`` if you want to use a custom BaseForm subclass.
  74
+
  75
+    Provide ``formfield_callback`` if you want to define different logic for
  76
+    determining the formfield for a given database field. It's a callable that
  77
+    takes a database Field instance, plus **kwargs, and returns a form Field
  78
+    instance with the given kwargs (i.e. 'initial').
70 79
     """
71 80
     model = instance.__class__
72 81
     opts = model._meta
73 82
     field_list = []
74 83
     for f in opts.fields + opts.many_to_many:
75 84
         current_value = f.value_from_object(instance)
76  
-        formfield = f.formfield(initial=current_value)
  85
+        formfield = formfield_callback(f, initial=current_value)
77 86
         if formfield:
78 87
             field_list.append((f.name, formfield))
79 88
     fields = SortedDictFromList(field_list)

0 notes on commit c93d193

Please sign in to comment.
Something went wrong with that request. Please try again.