david / semanticdjango (http://semanticdjango.org/)
fork of django-trunk
The right time for a semantic contrib, that's my pony.
Clone this repository (size: 23.1 MB): HTTPS / SSH
$ hg clone http://code.welldev.org/semanticdjango
| commit 6483: | 8877745cfdb3 |
| parent 6482: | 7f485076b3e4 |
| branch: | default |
Fixed #10069 -- Fixed the model form unique validation code to not proceed with using, for example, RelatedObjects returned by get_field_by_name as though they were model Fields.
kmtracey
20 months ago
20 months ago
Changed (Δ660 bytes):
raw changeset »
django/forms/models.py (7 lines added, 1 lines removed)
tests/modeltests/model_forms/models.py (11 lines added, 0 lines removed)
Up to file-list django/forms/models.py:
| … | … | @@ -224,7 +224,7 @@ class BaseModelForm(BaseForm): |
224 |
224 |
return self.cleaned_data |
225 |
225 |
|
226 |
226 |
def validate_unique(self): |
227 |
from django.db.models.fields import FieldDoesNotExist |
|
227 |
from django.db.models.fields import FieldDoesNotExist, Field as ModelField |
|
228 |
228 |
|
229 |
229 |
# Gather a list of checks to perform. We only perform unique checks |
230 |
230 |
# for fields present and not None in cleaned_data. Since this is a |
| … | … | @@ -248,6 +248,12 @@ class BaseModelForm(BaseForm): |
248 |
248 |
except FieldDoesNotExist: |
249 |
249 |
# This is an extra field that's not on the ModelForm, ignore it |
250 |
250 |
continue |
251 |
if not isinstance(f, ModelField): |
|
252 |
# This is an extra field that happens to have a name that matches, |
|
253 |
# for example, a related object accessor for this model. So |
|
254 |
# get_field_by_name found it, but it is not a Field so do not proceed |
|
255 |
# to use it as if it were. |
|
256 |
continue |
|
251 |
257 |
if f.unique and self.cleaned_data.get(name) is not None: |
252 |
258 |
unique_checks.append((name,)) |
253 |
259 |
Up to file-list tests/modeltests/model_forms/models.py:
| … | … | @@ -193,6 +193,17 @@ Extra fields. |
193 |
193 |
>>> CategoryForm.base_fields.keys() |
194 |
194 |
['name', 'slug', 'url', 'some_extra_field'] |
195 |
195 |
|
196 |
Extra field that has a name collision with a related object accessor. |
|
197 |
||
198 |
>>> class WriterForm(ModelForm): |
|
199 |
... book = forms.CharField(required=False) |
|
200 |
... |
|
201 |
... class Meta: |
|
202 |
... model = Writer |
|
203 |
||
204 |
>>> wf = WriterForm({'name': 'Richard Lockridge'}) |
|
205 |
>>> wf.is_valid() |
|
206 |
True |
|
196 |
207 |
|
197 |
208 |
Replacing a field. |
198 |
209 |
