title: RelatedObjectDoesNotExist: User has no userprofile
body: <p>So I've extended my user with the field <code>score</code> like this:</p>
  
  <p>models.py:</p>
  
  <pre><code>class UserProfile(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE)
      score = models.IntegerField(default=0);
  </code></pre>
  
  <p>which seemed to be along the lines of the <a href="https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-the-existing-user-model" rel="noreferrer">recommended way</a>.</p>
  
  <p>Then I tried to access the user's <code>userprofile</code> in my views:</p>
  
  <p><strong>views.py:</strong></p>
  
  <pre><code>player = request.user.userprofile
  </code></pre>
  
  <p>which seemed to align with the recommended way as well, but that is where I get my error:</p>
  
  <pre><code>RelatedObjectDoesNotExist
  User has no userprofile.
  </code></pre>
  
  <p>If I change <code>userprofile</code> to something else I get another error:  </p>
  
  <pre><code>AttributeError
  'User' object has no attribute 'flowerpot'
  </code></pre>
  
  <p>When I try the following code:</p>
  
  <pre><code>print request.user
  print UserProfile.objects.all()
  </code></pre>
  
  <p>I get the console output:</p>
  
  <pre><code>post_test1
  []
  </code></pre>
  
  <p><strong>EDIT</strong><br>
  I have two superusers, seven users I created before extending the user, and one user (post_test1) that I created after extending the user.  </p>
  
  <hr>
  
  <p><strong>EDIT 2</strong> </p>
  
  <p>It seams clear that what I need is to create a <code>post_save</code> handler that creates a new profile whenever the User object is created.</p>
  
  <p>This seemed simple enough when I read it, went to the <a href="https://docs.djangoproject.com/en/1.9/ref/signals/" rel="noreferrer">page</a> that was linked to, which was a list of all the signals Django sends. I looked up <code>post_save</code> and it said:</p>
  
  <blockquote>
    <p>Like pre_save, but sent at the end of the save() method.</p>
  </blockquote>
  
  <p>Alright, so I look up <code>pre_save</code> and it says:</p>
  
  <blockquote>
    <p>This is sent at the beginning of a model’s save() method.</p>
  </blockquote>
  
  <p>I've interpreted it like this: When I create my user (in my <code>views.py</code>) the <code>save()</code> method should be called, which hasn't been the case up until now, and after that a <code>post_save</code> should be sent(?), which will create a new profile whenever the User object is created!</p>
  
  <p>So now I'm ready to start looking at examples, so I google:</p>
  
  <p><code>django post save example</code><br>
  <a href="https://stackoverflow.com/a/13016982/4136445">Here</a> it looks like I'm supposed to add something that looks like a decorator <code>@receiver(post_save, ...</code><br>
  <a href="https://stackoverflow.com/questions/5658745/django-registration-auto-create-userprofile">Here</a> it looks like I'm supposed to alter multiple files and write a signal definition?<br>
  <a href="https://stackoverflow.com/questions/1910359/creating-a-extended-user-profile">This one</a> also seems to imply multiple files (including a <code>signals.py</code>)</p>
  
  <p>It looks like there's a lot more to it than I first thought. Could anyone here either explain how I'm to do this or show me to some good resources on how signals work?</p>
  
  <p>Right now my <code>create_user</code> view look like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>def create_user(request):
      if request.method == "POST":
          form = UserCreationForm(request.POST)
          if form.is_valid():
              username = form.cleaned_data["username"]
              password = form.cleaned_data["password1"]
              new_user = User.objects.create_user(username=username, password=password)
              return redirect('play')
      else:
          form = UserCreationForm()
  
      return render(request, 'antonymapp/create_user.html', {'form': form})
  </code></pre>
  
  <p>Should I call <code>new_user.save()</code> before returning? If yes, why has it worked up until now? I have a bunch of users that I've created while testing this view. It also looks like somewhere around here <code>post_save.connect(create_profile, sender=User)</code> should be added?</p>