Code

Ticket #5634: django.newforms.models.py.diff

File django.newforms.models.py.diff, 1.4 KB (added by MarioGonzalez <gonzalemario @…>, 7 years ago)
Line 
1Index: django/newforms/models.py
2===================================================================
3--- django/newforms/models.py   (revision 6431)
4+++ django/newforms/models.py   (working copy)
5@@ -66,7 +66,7 @@
6         return save_instance(self, instance, fields, fail_message, commit)
7     return save
8 
9-def form_for_model(model, form=BaseForm, fields=None, formfield_callback=lambda f: f.formfield()):
10+def form_for_model(model, form=BaseForm, fields=None, queryset=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
11     """
12     Returns a Form class for the given Django model class.
13 
14@@ -76,6 +76,7 @@
15     determining the formfield for a given database field. It's a callable that
16     takes a database Field instance and returns a form Field instance.
17     """
18+    from django.db.models.fields.related import ManyToManyField
19     opts = model._meta
20     field_list = []
21     for f in opts.fields + opts.many_to_many:
22@@ -83,7 +84,10 @@
23             continue
24         if fields and not f.name in fields:
25             continue
26-        formfield = formfield_callback(f)
27+        if isinstance(f, ManyToManyField) and queryset is not None:
28+            formfield = formfield_callback(f, queryset=queryset)
29+        else:
30+            formfield = formfield_callback(f)
31         if formfield:
32             field_list.append((f.name, formfield))
33     base_fields = SortedDictFromList(field_list)