@@ -122,21 +122,50 @@ class and an AdminSite instance, so let's just go ahead and do that manually
122
122
>>> type(ma.get_form(request).base_fields['sign_date'].widget)
123
123
<class 'django.contrib.admin.widgets.AdminDateWidget'>
124
124
125
+ If we need to override the queryset of a ModelChoiceField in our custom form
126
+ make sure that RelatedFieldWidgetWrapper doesn't mess that up.
127
+
128
+ >>> band2 = Band(name='The Beetles', bio='', sign_date=date(1962, 1, 1))
129
+ >>> band2.save()
130
+
131
+ >>> class AdminConcertForm(forms.ModelForm):
132
+ ... class Meta:
133
+ ... model = Concert
134
+ ...
135
+ ... def __init__(self, *args, **kwargs):
136
+ ... super(AdminConcertForm, self).__init__(*args, **kwargs)
137
+ ... self.fields["main_band"].queryset = Band.objects.filter(name='The Doors')
138
+
139
+ >>> class ConcertAdmin(ModelAdmin):
140
+ ... form = AdminConcertForm
141
+
142
+ >>> ma = ConcertAdmin(Concert, site)
143
+ >>> form = ma.get_form(request)()
144
+ >>> print form["main_band"]
145
+ <select name="main_band" id="id_main_band">
146
+ <option value="" selected="selected">---------</option>
147
+ <option value="1">The Doors</option>
148
+ </select>
149
+
150
+ >>> band2.delete()
151
+
125
152
# radio_fields behavior ################################################
126
153
127
154
First, without any radio_fields specified, the widgets for ForeignKey
128
155
and fields with choices specified ought to be a basic Select widget.
129
- For Select fields, all of the choices lists have a first entry of dashes.
156
+ ForeignKey widgets in the admin are wrapped with RelatedFieldWidgetWrapper so
157
+ they need to be handled properly when type checking. For Select fields, all of
158
+ the choices lists have a first entry of dashes.
130
159
131
160
>>> cma = ModelAdmin(Concert, site)
132
161
>>> cmafa = cma.get_form(request)
133
162
134
- >>> type(cmafa.base_fields['main_band'].widget)
163
+ >>> type(cmafa.base_fields['main_band'].widget.widget )
135
164
<class 'django.newforms.widgets.Select'>
136
165
>>> list(cmafa.base_fields['main_band'].widget.choices)
137
166
[(u'', u'---------'), (1, u'The Doors')]
138
167
139
- >>> type(cmafa.base_fields['opening_band'].widget)
168
+ >>> type(cmafa.base_fields['opening_band'].widget.widget )
140
169
<class 'django.newforms.widgets.Select'>
141
170
>>> list(cmafa.base_fields['opening_band'].widget.choices)
142
171
[(u'', u'---------'), (1, u'The Doors')]
@@ -152,7 +181,7 @@ class and an AdminSite instance, so let's just go ahead and do that manually
152
181
[('', '---------'), (1, 'Plane'), (2, 'Train'), (3, 'Bus')]
153
182
154
183
Now specify all the fields as radio_fields. Widgets should now be
155
- RadioSelect, and the choices list should have a first entry of 'None' iff
184
+ RadioSelect, and the choices list should have a first entry of 'None' if
156
185
blank=True for the model field. Finally, the widget should have the
157
186
'radiolist' attr, and 'inline' as well if the field is specified HORIZONTAL.
158
187
@@ -167,14 +196,14 @@ class and an AdminSite instance, so let's just go ahead and do that manually
167
196
>>> cma = ConcertAdmin(Concert, site)
168
197
>>> cmafa = cma.get_form(request)
169
198
170
- >>> type(cmafa.base_fields['main_band'].widget)
199
+ >>> type(cmafa.base_fields['main_band'].widget.widget )
171
200
<class 'django.contrib.admin.widgets.AdminRadioSelect'>
172
201
>>> cmafa.base_fields['main_band'].widget.attrs
173
202
{'class': 'radiolist inline'}
174
203
>>> list(cmafa.base_fields['main_band'].widget.choices)
175
204
[(1, u'The Doors')]
176
205
177
- >>> type(cmafa.base_fields['opening_band'].widget)
206
+ >>> type(cmafa.base_fields['opening_band'].widget.widget )
178
207
<class 'django.contrib.admin.widgets.AdminRadioSelect'>
179
208
>>> cmafa.base_fields['opening_band'].widget.attrs
180
209
{'class': 'radiolist'}
0 commit comments