Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7880 -- Corrected the handling of fields in the admin that spe…

…cify choices, so that the presence of choices overrides the decision to use a custom admin widget. This is primarily of interest to Date/Time fields. Thanks to camilonova for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8153 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f7d123a731e62af9a792623812bef632f3142547 1 parent a98d12b
Russell Keith-Magee authored

Showing 1 changed file with 17 additions and 9 deletions. Show diff stats Hide diff stats

  1. 26  django/contrib/admin/options.py
26  django/contrib/admin/options.py
@@ -129,6 +129,23 @@ def formfield_for_dbfield(self, db_field, **kwargs):
129 129
 
130 130
         If kwargs are given, they're passed to the form Field's constructor.
131 131
         """
  132
+    
  133
+        # If the field specifies choices, we don't need to look for special
  134
+        # admin widgets - we just need to use a select widget of some kind.
  135
+        if db_field.choices:
  136
+            if db_field.name in self.radio_fields:
  137
+                # If the field is named as a radio_field, use a RadioSelect
  138
+                kwargs['widget'] = widgets.AdminRadioSelect(
  139
+                    choices=db_field.get_choices(include_blank=db_field.blank,
  140
+                        blank_choice=[('', _('None'))]),
  141
+                    attrs={
  142
+                        'class': get_ul_class(self.radio_fields[db_field.name]),
  143
+                    }
  144
+                )
  145
+            else:
  146
+                # Otherwise, use the default select widget.
  147
+                return db_field.formfield(**kwargs)
  148
+        
132 149
         # For DateTimeFields, use a special field and widget.
133 150
         if isinstance(db_field, models.DateTimeField):
134 151
             kwargs['form_class'] = forms.SplitDateTimeField
@@ -176,15 +193,6 @@ def formfield_for_dbfield(self, db_field, **kwargs):
176 193
             if not db_field.name in self.raw_id_fields:
177 194
                 formfield.widget = widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field.rel, self.admin_site)
178 195
             return formfield
179  
-        
180  
-        if db_field.choices and db_field.name in self.radio_fields:
181  
-            kwargs['widget'] = widgets.AdminRadioSelect(
182  
-                choices=db_field.get_choices(include_blank=db_field.blank,
183  
-                    blank_choice=[('', _('None'))]),
184  
-                attrs={
185  
-                    'class': get_ul_class(self.radio_fields[db_field.name]),
186  
-                }
187  
-            )
188 196
 
189 197
         # For any other type of field, just call its formfield() method.
190 198
         return db_field.formfield(**kwargs)

0 notes on commit f7d123a

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