Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#10069 closed (fixed)

'RelatedObject' object has no attribute 'unique' thrown by ModelForm validation

Reported by: Karen Tracey Owned by: Karen Tracey
Component: Forms Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Reported here: http://groups.google.com/group/django-users/browse_thread/thread/210298adf4a0f280

More easily recreatable by creating a ModelForm for the tutorial Poll model that looks like this:

class PollForm(forms.ModelForm):
    choice = forms.CharField(required=False)
    class Meta:
        model = Poll
        exclude = ('pub_date',)

and then from manage.py shell:

>>> from polls.models import Poll, PollForm
>>> pf = PollForm(data={'question': "Where's the beef?"})
>>> pf.is_valid()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/kmt/tmp/django/trunk/django/forms/forms.py", line 120, in is_valid
    return self.is_bound and not bool(self.errors)
  File "/home/kmt/tmp/django/trunk/django/forms/forms.py", line 111, in _get_errors
    self.full_clean()
  File "/home/kmt/tmp/django/trunk/django/forms/forms.py", line 241, in full_clean
    self.cleaned_data = self.clean()
  File "/home/kmt/tmp/django/trunk/django/forms/models.py", line 223, in clean
    self.validate_unique()
  File "/home/kmt/tmp/django/trunk/django/forms/models.py", line 251, in validate_unique
    if f.unique and self.cleaned_data.get(name) is not None:
AttributeError: 'RelatedObject' object has no attribute 'unique'

Problem is the added 'choice' field on the form is not actually a Poll model field, but is findable via Poll's _meta.get_field_by_name due to the Choice model having a ForeinKey pointing to Poll. Fix is simple, I think, but I'm creating the ticket so someone searching on the problem in the future might find that it once existed and when it was fixed.

Change History (3)

comment:1 by Karen Tracey, 15 years ago

Owner: changed from nobody to Karen Tracey
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:2 by Karen Tracey, 15 years ago

Resolution: fixed
Status: assignedclosed

(In [9777]) 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.

comment:3 by Karen Tracey, 15 years ago

(In [9778]) [1.0.X] 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.

r9777 from trunk.

Note: See TracTickets for help on using tickets.
Back to Top