title: Django - RelatedObjectDoesNotExist error after post_save creation signal
body: <p>I have general user profile model which has several roles - Client, Translator and Editor. I'm using a one-to-one relation with the extended user model I've created in accounts app and I'm using Django post_save signal to automatically create a profile instance related to the user which works just fine.
  Now here is the problem: I need to have role-specific profile model like client profile, translator profile, etc. and again I'm trying to use Django post_save signal to create a {role} profile object related to the main UserProfile model and I'm getting the error below:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;D:\Django\tct\venv\lib\site-packages\django\core\handlers\exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\core\handlers\base.py&quot;, line 181, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\contrib\admin\options.py&quot;, line 614, in wrapper
      return self.admin_site.admin_view(view)(*args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\utils\decorators.py&quot;, line 130, in _wrapped_view
      response = view_func(request, *args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\views\decorators\cache.py&quot;, line 44, in _wrapped_view_func
      response = view_func(request, *args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\contrib\admin\sites.py&quot;, line 233, in inner
      return view(request, *args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\utils\decorators.py&quot;, line 43, in _wrapper
      return bound_method(*args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\views\decorators\debug.py&quot;, line 89, in sensitive_post_parameters_wrapper
      return view(request, *args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\utils\decorators.py&quot;, line 43, in _wrapper
      return bound_method(*args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\utils\decorators.py&quot;, line 130, in _wrapped_view
      response = view_func(request, *args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\contrib\auth\admin.py&quot;, line 99, in add_view
      return self._add_view(request, form_url, extra_context)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\contrib\auth\admin.py&quot;, line 126, in _add_view
      return super().add_view(request, form_url, extra_context)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\contrib\admin\options.py&quot;, line 1653, in add_view
      return self.changeform_view(request, None, form_url, extra_context)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\utils\decorators.py&quot;, line 43, in _wrapper
      return bound_method(*args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\utils\decorators.py&quot;, line 130, in _wrapped_view
      response = view_func(request, *args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\contrib\admin\options.py&quot;, line 1534, in changeform_view
      return self._changeform_view(request, object_id, form_url, extra_context)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\contrib\admin\options.py&quot;, line 1580, in _changeform_view
      self.save_model(request, new_object, form, not add)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\contrib\admin\options.py&quot;, line 1093, in save_model
      obj.save()
    File &quot;D:\Django\tct\venv\lib\site-packages\django\contrib\auth\base_user.py&quot;, line 67, in save
      super().save(*args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\db\models\base.py&quot;, line 753, in save
      self.save_base(using=using, force_insert=force_insert,
    File &quot;D:\Django\tct\venv\lib\site-packages\django\db\models\base.py&quot;, line 801, in save_base
      post_save.send(
    File &quot;D:\Django\tct\venv\lib\site-packages\django\dispatch\dispatcher.py&quot;, line 177, in send
      return [
    File &quot;D:\Django\tct\venv\lib\site-packages\django\dispatch\dispatcher.py&quot;, line 178, in &lt;listcomp&gt;
      (receiver, receiver(signal=self, sender=sender, **named))
    File &quot;D:\Django\tct\src\profiles\models.py&quot;, line 73, in create_user_profile
      UserProfile.objects.create(user=instance)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\db\models\manager.py&quot;, line 85, in manager_method
      return getattr(self.get_queryset(), name)(*args, **kwargs)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\db\models\query.py&quot;, line 447, in create
      obj.save(force_insert=True, using=self.db)
    File &quot;D:\Django\tct\venv\lib\site-packages\django\db\models\base.py&quot;, line 753, in save
      self.save_base(using=using, force_insert=force_insert,
    File &quot;D:\Django\tct\venv\lib\site-packages\django\db\models\base.py&quot;, line 801, in save_base
      post_save.send(
    File &quot;D:\Django\tct\venv\lib\site-packages\django\dispatch\dispatcher.py&quot;, line 177, in send
      return [
    File &quot;D:\Django\tct\venv\lib\site-packages\django\dispatch\dispatcher.py&quot;, line 178, in &lt;listcomp&gt;
      (receiver, receiver(signal=self, sender=sender, **named))
    File &quot;D:\Django\tct\src\clients\models.py&quot;, line 32, in save_client_profile
      instance.clientprofile.save()
    File &quot;D:\Django\tct\venv\lib\site-packages\django\db\models\fields\related_descriptors.py&quot;, line 421, in __get__
      raise self.RelatedObjectDoesNotExist(
  
  Exception Type: RelatedObjectDoesNotExist at /admin/accounts/user/add/
  Exception Value: UserProfile has no clientprofile.
  </code></pre>
  <h3>User Model:</h3>
  <pre><code>class User(AbstractUser):
      id = models.UUIDField(
          _(&quot;User ID&quot;), primary_key=True, default=uuid.uuid4, editable=False
      )
  </code></pre>
  <h3>General Profile Model</h3>
  <pre><code>class UserProfile(models.Model):
      # General Info
      CLIENT = 1
      TRANSLATOR = 2
      EDITOR = 3
      ROLE_CHOICES = (
          (CLIENT, _(&quot;Client&quot;)),
          (TRANSLATOR, _(&quot;Translator&quot;)),
          (EDITOR, _(&quot;Editor&quot;)),
      )
      MALE = 1
      FEMALE = 2
      GENDER_CHOICES = ((MALE, _(&quot;Male&quot;)), (FEMALE, _(&quot;Female&quot;)))
      user = models.OneToOneField(
          User, verbose_name=_(&quot;User&quot;), on_delete=models.CASCADE, primary_key=True
      )
      role = models.PositiveSmallIntegerField(
          _(&quot;Role&quot;), choices=ROLE_CHOICES, null=True, blank=True
      )
      bio = models.TextField(_(&quot;Bio&quot;), null=True, blank=True)
      avatar = models.ImageField(
          _(&quot;Avatar&quot;), upload_to=&quot;profiles/profile/avatar/&quot;, blank=True
      )
      gender = models.PositiveSmallIntegerField(
          _(&quot;Gender&quot;), choices=GENDER_CHOICES, null=True, blank=True
      )
  
      # Contact Info
      phone = models.CharField(_(&quot;Phone&quot;), max_length=20, null=True, blank=True)
      cell = models.CharField(_(&quot;Cell&quot;), max_length=20, null=True, blank=True)
      id_number = models.CharField(_(&quot;ID Number&quot;), max_length=20, null=True, blank=True)
      address = models.TextField(_(&quot;Address&quot;), null=True, blank=True)
      postal_code = models.CharField(
          _(&quot;Postal Code&quot;), max_length=20, null=True, blank=True
      )
      website_url = models.URLField(
          _(&quot;Website URL&quot;), max_length=200, null=True, blank=True
      )
      instagram_username = models.CharField(
          _(&quot;Instagram Username&quot;), max_length=200, null=True, blank=True
      )
      telegram_username = models.CharField(
          _(&quot;Telegram Username&quot;), max_length=200, null=True, blank=True
      )
      twitter_username = models.CharField(
          _(&quot;Twitter Username&quot;), max_length=200, null=True, blank=True
      )
  
      # Model Extra
      class Meta:
          verbose_name = _(&quot;User Profile&quot;)
          verbose_name_plural = _(&quot;User Profile&quot;)
  
      def __str__(self):
          return self.user.username
  
      def get_absolute_url(self):
          return reverse(&quot;profiles:user_profile_detail&quot;, args=[str(self.user.id)])
  
  
  @receiver(post_save, sender=User)
  def create_user_profile(sender, instance, created, **kwargs):
      if created:
          UserProfile.objects.create(user=instance)
  
  
  @receiver(post_save, sender=User)
  def save_user_profile(sender, instance, **kwargs):
      instance.userprofile.save()
  </code></pre>
  <h3>Client Profile Model</h3>
  <pre><code>class ClientProfile(models.Model):
      profile = models.OneToOneField(
          UserProfile, verbose_name=_(&quot;User&quot;), on_delete=models.CASCADE, primary_key=True
      )
      credit = models.IntegerField(_(&quot;Credit&quot;), default=0, blank=True)
      debt = models.IntegerField(_(&quot;Debt&quot;), default=0, blank=True)
      referral_code = ULIDField(default=default)
      referrals = models.ManyToManyField(
          User, verbose_name=_(&quot;Referrals&quot;), related_name=&quot;client_profile&quot;
      )
      income = models.IntegerField(_(&quot;Income&quot;), default=0, blank=True)
  
  @receiver(post_save, sender=UserProfile)
  def create_client_profile(sender, instance, created, **kwargs):
      if created:
          if instance.role == 1:
              ClientProfile.objects.create(profile=instance)
  
  
  @receiver(post_save, sender=UserProfile)
  def save_client_profile(sender, instance, **kwargs):
      instance.clientprofile.save()
  </code></pre>
  <h3>Custom sign up form with the role field</h3>
  <pre><code>class CustomSignupForm(SignupForm):
      role = forms.ChoiceField(
          widget=forms.RadioSelect(), choices=UserProfile.ROLE_CHOICES, required=False, label=_(&quot;Role&quot;)
      )
  
      def save(self, request):
  
          # Ensure you call the parent class's save.
          # .save() returns a User object.
          user = super(CustomSignupForm, self).save(request)
          user.userprofile.role = self.cleaned_data.get(&quot;role&quot;)
          user.save()
          # You must return the original result.
          return user
  </code></pre>