Code

Ticket #3987: diff-dynamic.2.diff

File diff-dynamic.2.diff, 7.6 KB (added by Baptiste <baptiste.goupil@…>, 7 years ago)

oops, fixed a little error

Line 
1Index: db/models/fields/related.py
2===================================================================
3--- db/models/fields/related.py (revision 5636)
4+++ db/models/fields/related.py (working copy)
5@@ -544,7 +544,10 @@
6         setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related))
7 
8     def formfield(self, **kwargs):
9-        defaults = {'form_class': forms.ModelChoiceField, 'queryset': self.rel.to._default_manager.all()}
10+        defaults = {'form_class': forms.ModelChoiceField}
11+        #To prevent an useless query
12+        if not 'queryset' in kwargs:
13+            defaults['queryset'] = self.rel.to._default_manager.all()
14         defaults.update(kwargs)
15         return super(ForeignKey, self).formfield(**defaults)
16 
17@@ -605,7 +608,9 @@
18             cls._meta.one_to_one_field = self
19 
20     def formfield(self, **kwargs):
21-        defaults = {'form_class': forms.ModelChoiceField, 'queryset': self.rel.to._default_manager.all()}
22+        defaults = {'form_class': forms.ModelChoiceField}
23+        if not 'queryset' in kwargs:
24+            defaults['queryset'] = self.rel.to._default_manager.all()
25         defaults.update(kwargs)
26         return super(OneToOneField, self).formfield(**defaults)
27 
28@@ -711,7 +716,9 @@
29         return getattr(obj, self.attname).all()
30 
31     def formfield(self, **kwargs):
32-        defaults = {'form_class': forms.ModelMultipleChoiceField, 'queryset': self.rel.to._default_manager.all()}
33+        defaults = {'form_class': forms.ModelMultipleChoiceField}
34+        if not 'queryset' in kwargs:
35+            defaults['queryset'] = self.rel.to._default_manager.all()
36         defaults.update(kwargs)
37         # If initial is passed in, it's a list of related objects, but the
38         # MultipleChoiceField takes a list of IDs.
39Index: contrib/admin/options.py
40===================================================================
41--- contrib/admin/options.py    (revision 5636)
42+++ contrib/admin/options.py    (working copy)
43@@ -103,7 +103,7 @@
44     raw_id_fields = ()
45     fields = None
46 
47-    def formfield_for_dbfield(self, db_field, **kwargs):
48+    def formfield_for_dbfield(self, db_field, request, **kwargs):
49         """
50         Hook for specifying the form Field instance for a given database Field
51         instance.
52@@ -113,7 +113,8 @@
53         # For ManyToManyFields with a filter interface, use a special widget.
54         if isinstance(db_field, models.ManyToManyField) and db_field.name in (self.filter_vertical + self.filter_horizontal):
55             kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))
56-            return db_field.formfield(**kwargs)
57+            if hasattr(self, 'dynamic_%s_choices' % db_field.name):
58+                formfield = db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs)
59 
60         # For DateTimeFields, use a special field and widget.
61         if isinstance(db_field, models.DateTimeField):
62@@ -133,15 +134,20 @@
63 
64         # For ForeignKey or ManyToManyFields, use a special widget.
65         if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
66+            if hasattr(self, 'dynamic_%s_choices' % db_field.name):
67+                formfield = db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs)
68+            else:
69+                formfield = db_field.formfield(**kwargs)
70             if isinstance(db_field, models.ForeignKey) and db_field.name in self.raw_id_fields:
71-                kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.rel)
72-                return db_field.formfield(**kwargs)
73+                formfield.widget = widgets.ForeignKeyRawIdWidget(db_field.rel)
74             else:
75                 # Wrap the widget's render() method with a method that adds
76                 # extra HTML to the end of the rendered output.
77-                formfield = db_field.formfield(**kwargs)
78                 formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel)
79-                return formfield
80+            return formfield
81+       
82+        if isinstance(db_field, (models.OneToOneField)) and hasattr(self, 'dynamic_%s_choices' % db_field.name):
83+            return db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs)
84 
85         # For any other type of field, just call its formfield() method.
86         return db_field.formfield(**kwargs)
87@@ -414,7 +420,7 @@
88             # Object list will give 'Permission Denied', so go back to admin home
89             post_url = '../../../'
90 
91-        ModelForm = forms.form_for_model(model, formfield_callback=self.formfield_for_dbfield)
92+        ModelForm = forms.form_for_model(model, formfield_callback=lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs))
93 
94         inline_formsets = []
95         if request.POST:
96@@ -422,14 +428,14 @@
97             if opts.has_field_type(models.FileField):
98                 new_data.update(request.FILES)
99             form = ModelForm(new_data)
100-            for FormSet in self.get_inline_formsets():
101+            for FormSet in self.get_inline_formsets(request):
102                 inline_formset = FormSet(data=new_data)
103                 inline_formsets.append(inline_formset)
104             if all_valid(inline_formsets) and form.is_valid():
105                 return self.save_add(request, model, form, inline_formsets, '../%s/')
106         else:
107             form = ModelForm(initial=request.GET)
108-            for FormSet in self.get_inline_formsets():
109+            for FormSet in self.get_inline_formsets(request):
110                 inline_formset = FormSet()
111                 inline_formsets.append(inline_formset)
112 
113@@ -467,7 +473,7 @@
114         if request.POST and request.POST.has_key("_saveasnew"):
115             return self.add_view(request, form_url='../../add/')
116 
117-        ModelForm = forms.form_for_instance(obj, formfield_callback=self.formfield_for_dbfield)
118+        ModelForm = forms.form_for_instance(obj, formfield_callback=lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs))
119 
120         inline_formsets = []
121         if request.POST:
122@@ -475,7 +481,7 @@
123             if opts.has_field_type(models.FileField):
124                 new_data.update(request.FILES)
125             form = ModelForm(new_data)
126-            for FormSet in self.get_inline_formsets():
127+            for FormSet in self.get_inline_formsets(request):
128                 inline_formset = FormSet(obj, new_data)
129                 inline_formsets.append(inline_formset)
130 
131@@ -483,7 +489,7 @@
132                 return self.save_change(request, model, form, inline_formsets)
133         else:
134             form = ModelForm()
135-            for FormSet in self.get_inline_formsets():
136+            for FormSet in self.get_inline_formsets(request):
137                 inline_formset = FormSet(obj)
138                 inline_formsets.append(inline_formset)
139 
140@@ -610,10 +616,10 @@
141         ]
142         return render_to_response(template_list, extra_context, context_instance=template.RequestContext(request))
143 
144-    def get_inline_formsets(self):
145+    def get_inline_formsets(self, request):
146         inline_formset_classes = []
147         for opts in self.inlines:
148-            inline = inline_formset(self.model, opts.model, formfield_callback=opts.formfield_for_dbfield, fields=opts.fields, extra=opts.extra)
149+            inline = inline_formset(self.model, opts.model, formfield_callback=lambda f, **kwargs: opts.formfield_for_dbfield(f, request, **kwargs), fields=opts.fields, extra=opts.extra)
150             inline_formset_classes.append(inline)
151         return inline_formset_classes
152