Django

Code

Ticket #10069 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

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

Reported by: kmtracey Assigned to: kmtracey
Milestone: Component: Forms
Version: 1.0 Keywords:
Cc: Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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.

Attachments

Change History

01/19/09 12:30:18 changed by kmtracey

  • status changed from new to assigned.
  • needs_better_patch changed.
  • needs_tests changed.
  • owner changed from nobody to kmtracey.
  • needs_docs changed.
  • stage changed from Unreviewed to Accepted.

01/19/09 13:37:31 changed by kmtracey

  • status changed from assigned to closed.
  • resolution set to fixed.

(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.

01/19/09 13:43:02 changed by kmtracey

(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.


Add/Change #10069 ('RelatedObject' object has no attribute 'unique' thrown by ModelForm validation)




Change Properties
Action