﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25474	RelatedObjectDoesNotExist caused by Model.clean()	André Avorio	nobody	"Hello,

I'm not sure what's going on here.

I have a model:

{{{
class Quote(models.Model):
    curation = models.ForeignKey(Curation)
    rental_period = models.PositiveSmallIntegerField('rental period (weeks)', null=True, blank=True, validators=[MinValueValidator(0),MaxValueValidator(260)])
    [...]

    @property
    def type(self):
        return self.curation.opportunity.type
}}}

And I decided to implement a validation using:

{{{
def clean(self):
        if self.type == 'rental':
            if self.rental_period is None:
                raise ValidationError({'rental_period': 'The rental period must be defined for rental quotes.'})
}}}

When I try to create a new Quote object in the admin, I now get a `RelatedObjectDoesNotExist` error saying `Quote has no curation` when I don't select any `curation` from the dropdown menu displayed. When I remove my `clean()` function and still do not select any `curation`, I get the normal error message in the admin UI.

Here's the full traceback:

{{{
Traceback:
File ""/.venv/lib/python3.4/site-packages/django/core/handlers/base.py"" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File ""/.venv/lib/python3.4/site-packages/django/contrib/admin/options.py"" in wrapper
  616.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File ""/.venv/lib/python3.4/site-packages/django/utils/decorators.py"" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File ""/.venv/lib/python3.4/site-packages/django/views/decorators/cache.py"" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File ""/.venv/lib/python3.4/site-packages/django/contrib/admin/sites.py"" in inner
  233.             return view(request, *args, **kwargs)
File ""/maestro/quotes/admin.py"" in add_view
  44.         return super(QuoteAdmin, self).add_view(request, form_url, extra_context)
File ""/.venv/lib/python3.4/site-packages/django/contrib/admin/options.py"" in add_view
  1516.         return self.changeform_view(request, None, form_url, extra_context)
File ""/.venv/lib/python3.4/site-packages/django/utils/decorators.py"" in _wrapper
  34.             return bound_func(*args, **kwargs)
File ""/.venv/lib/python3.4/site-packages/django/utils/decorators.py"" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File ""/.venv/lib/python3.4/site-packages/django/utils/decorators.py"" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/contextlib.py"" in inner
  30.                 return func(*args, **kwds)
File ""/.venv/lib/python3.4/site-packages/django/contrib/admin/options.py"" in changeform_view
  1459.             if form.is_valid():
File ""/.venv/lib/python3.4/site-packages/django/forms/forms.py"" in is_valid
  184.         return self.is_bound and not self.errors
File ""/.venv/lib/python3.4/site-packages/django/forms/forms.py"" in errors
  176.             self.full_clean()
File ""/.venv/lib/python3.4/site-packages/django/forms/forms.py"" in full_clean
  394.         self._post_clean()
File ""/.venv/lib/python3.4/site-packages/django/forms/models.py"" in _post_clean
  430.             self.instance.full_clean(exclude=exclude, validate_unique=False)
File ""/.venv/lib/python3.4/site-packages/django/db/models/base.py"" in full_clean
  1156.             self.clean()
File ""/maestro/quotes/models.py"" in clean
  131.         if self.curation != '':
File ""/.venv/lib/python3.4/site-packages/django/db/models/fields/related.py"" in __get__
  616.                 ""%s has no %s."" % (self.field.model.__name__, self.field.name)

Exception Type: RelatedObjectDoesNotExist at /admin/quotes/quote/add/
Exception Value: Quote has no curation.
}}}

Any clue on what's going on?

Many thanks."	Bug	closed	Uncategorized	1.8	Normal	worksforme			Unreviewed	0	0	0	0	0	0
