title: Django - Update or Insert based on query
body: <p>Learning how to use Django. I'm trying this code:</p>
  
  <pre><code>if form.is_valid() and form.has_changed():
      try:
          p = profile.objects.get(user_id= user.id)
          #Append data to profile record
                      profile_record.save()
      except p.DoesNotExist as e:
          #Create new profile record
          profile_record.save()
  </code></pre>
  
  <p>When <code>user_id</code> (FK field) isn't found I get an error:</p>
  
  <blockquote>
    <p>local variable 'p' referenced before assignment</p>
  </blockquote>
  
  <p>I've assumed that <code>p = ...</code> is the assignment?</p>
  
  <p>Thank you for any pointers as to what I'm doing wrong here.</p>