title: Why it happens - RelatedObjectDoesNotExist error caused by Model.clean()?
body: <p>I have a clean method that gives me RelatedObjectDoesNotExist  error in my model file </p>
  
  <pre><code>@with_author 
  class BOMVersion(models.Model): 
      version = IntegerVersionField( )
      name = models.CharField(max_length=200,null=True, blank=True)
      description = models.TextField(null=True, blank=True)
      material =  models.ForeignKey(Material)
      creation_time = models.DateTimeField(auto_now_add=True, blank=True)
      abovequantity = models.DecimalField(max_digits=19, decimal_places=10)
      belowquantity = models.DecimalField(max_digits=19, decimal_places=10)
      valid_from = models.DateTimeField(null=True, blank=True)
      valid_to = models.DateTimeField(null=True, blank=True)
      is_default = models.BooleanField(default=False)  
  
  
  
  
  
      def clean(self):
          model = self.__class__
          if model.objects.filter(material=self.material, is_default=True).count()  &gt; 1:
  
              raise ValidationError('Material {} has a defaul value already'.format(self.material))
  </code></pre>
  
  <p>UPDATE:
  Traceback </p>
  
  <pre><code>&gt; Traceback: File
  &gt; "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\handlers\base.py"
  &gt; in get_response
  &gt;   132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\I812624\dev\mrp\lib\site-packages\django\contrib\auth\decorators.py"
  &gt; in _wrapped_view
  &gt;   22.                 return view_func(request, *args, **kwargs) File "C:\Users\I812624\dev\mrp\src\item\views\viewsbomversion.py" in
  &gt; bomversion_new
  &gt;   68.         if form.is_valid(): File "C:\Users\I812624\dev\mrp\lib\site-packages\django\forms\forms.py" in
  &gt; is_valid
  &gt;   184.         return self.is_bound and not self.errors File "C:\Users\I812624\dev\mrp\lib\site-packages\django\forms\forms.py" in
  &gt; errors
  &gt;   176.             self.full_clean() File "C:\Users\I812624\dev\mrp\lib\site-packages\django\forms\forms.py" in
  &gt; full_clean
  &gt;   394.         self._post_clean() File "C:\Users\I812624\dev\mrp\lib\site-packages\django\forms\models.py" in
  &gt; _post_clean
  &gt;   430.             self.instance.full_clean(exclude=exclude, validate_unique=False) File
  &gt; "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\models\base.py"
  &gt; in full_clean
  &gt;   1132.             self.clean() File "C:\Users\I812624\dev\mrp\src\item\models.py" in clean
  &gt;   673.         if model.objects.filter(material=self.material, is_default=True).count()  &gt; 1: File
  &gt; "C:\Users\I812624\dev\mrp\lib\site-packages\django\db\models\fields\related.py"
  &gt; in __get__
  &gt;   608.                 "%s has no %s." % (self.field.model.__name__, self.field.name)
  &gt; 
  &gt; Exception Type: RelatedObjectDoesNotExist at
  &gt; /item/bomversion/new/1/http://127.0.0.1:8000/item/material/material_bomversion_details/1/
  &gt; Exception Value: BOMVersion has no material.
  </code></pre>
  
  <p>It looks like material is not visible from within clean method.
  It is ok if I remove the material from the validation and leave only is_default field.</p>
  
  <p>I am clueless why.....</p>
  
  <p>UPDATE:</p>
  
  <p>the problem is probably that material value is not coming from the form but gets populated in the view</p>
  
  <pre><code> if form.is_valid():
              bomversion= form.save(commit=False)
              bomversion.creation_time = timezone.now()
              bomversion.material = material
              bomversion.save()
  </code></pre>