Code

Ticket #10349: manytomany_dynamic_initial_against_1.0.2.diff

File manytomany_dynamic_initial_against_1.0.2.diff, 777 bytes (added by fas, 5 years ago)

simple fix

Line 
1Index: django/db/models/fields/related.py
2===================================================================
3--- django/db/models/fields/related.py  (revision 9565)
4+++ django/db/models/fields/related.py  (working copy)
5@@ -916,7 +916,10 @@
6         # If initial is passed in, it's a list of related objects, but the
7         # MultipleChoiceField takes a list of IDs.
8         if defaults.get('initial') is not None:
9-            defaults['initial'] = [i._get_pk_val() for i in defaults['initial']]
10+            initial = defaults['initial']
11+            if callable(initial):
12+                initial = initial()
13+            defaults['initial'] = [i._get_pk_val() for i in initial]
14         return super(ManyToManyField, self).formfield(**defaults)
15 
16     def db_type(self):