Ticket #3987: diff.txt

File diff.txt, 4.3 KB (added by Xniver, 7 years ago)

updated, works with revision 8502

Line 
1Index: django/contrib/admin/options.py
2===================================================================
3--- django/contrib/admin/options.py     (wersja 8502)
4+++ django/contrib/admin/options.py     (kopia robocza)
5@@ -38,7 +38,7 @@
6     radio_fields = {}
7     prepopulated_fields = {}
8 
9-    def formfield_for_dbfield(self, db_field, **kwargs):
10+    def formfield_for_dbfield(self, db_field, request, **kwargs):
11         """
12         Hook for specifying the form Field instance for a given database Field
13         instance.
14@@ -106,26 +106,29 @@
15 
16         # For ForeignKey or ManyToManyFields, use a special widget.
17         if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
18+            if hasattr(self, 'dynamic_%s_choices' % db_field.name):
19+                formfield = db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs)
20+            else:
21+                formfield = db_field.formfield(**kwargs)
22             if isinstance(db_field, models.ForeignKey) and db_field.name in self.raw_id_fields:
23-                kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.rel)
24+                formfield.widget = widgets.ForeignKeyRawIdWidget(db_field.rel)
25             elif isinstance(db_field, models.ForeignKey) and db_field.name in self.radio_fields:
26-                kwargs['widget'] = widgets.AdminRadioSelect(attrs={
27+                formfield.widget = widgets.AdminRadioSelect(attrs={
28                     'class': get_ul_class(self.radio_fields[db_field.name]),
29                 })
30-                kwargs['empty_label'] = db_field.blank and _('None') or None
31+                formfield.empty_label = db_field.blank and _('None') or None
32             else:
33                 if isinstance(db_field, models.ManyToManyField):
34                     # If it uses an intermediary model, don't show field in admin.
35                     if db_field.rel.through is not None:
36                         return None
37                     elif db_field.name in self.raw_id_fields:
38-                        kwargs['widget'] = widgets.ManyToManyRawIdWidget(db_field.rel)
39-                        kwargs['help_text'] = ''
40+                        formfield.widget = widgets.ManyToManyRawIdWidget(db_field.rel)
41+                        formfield.help_text = ''
42                     elif db_field.name in (list(self.filter_vertical) + list(self.filter_horizontal)):
43-                        kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))
44+                        formfield.widget = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))
45             # Wrap the widget's render() method with a method that adds
46             # extra HTML to the end of the rendered output.
47-            formfield = db_field.formfield(**kwargs)
48             # Don't wrap raw_id fields. Their add function is in the popup window.
49             if not db_field.name in self.raw_id_fields:
50                 # formfield can be None if it came from a OneToOneField with
51@@ -134,6 +137,9 @@
52                     formfield.widget = widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field.rel, self.admin_site)
53             return formfield
54 
55+        if isinstance(db_field, (models.OneToOneField)) and hasattr(self, 'dynamic_%s_choices' % db_field.name):
56+            return db_field.formfield(queryset = getattr(self, 'dynamic_%s_choices' % db_field.name)(request, db_field.rel.to), **kwargs)
57+           
58         # For any other type of field, just call its formfield() method.
59         return db_field.formfield(**kwargs)
60 
61@@ -262,7 +268,7 @@
62         defaults = {
63             "form": self.form,
64             "fields": fields,
65-            "formfield_callback": self.formfield_for_dbfield,
66+            "formfield_callback": lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs),
67         }
68         defaults.update(kwargs)
69         return modelform_factory(self.model, **defaults)
70@@ -770,7 +776,7 @@
71             "formset": self.formset,
72             "fk_name": self.fk_name,
73             "fields": fields,
74-            "formfield_callback": self.formfield_for_dbfield,
75+            "formfield_callback": lambda f, **kwargs: self.formfield_for_dbfield(f, request, **kwargs),
76             "extra": self.extra,
77             "max_num": self.max_num,
78         }
Back to Top