title: Django ModelForm not saving data to database
body: <p>A Django beginner here having a lot of trouble getting forms working. Yes I've worked through the tutorial and browsed the web a lot - what I have is mix of what I'm finding here and at other sites.  I'm using Python 2.7 and Django 1.5.  (although the official documentation is extensive it tends to assume you know most of it already - not good as a beginners reference or even an advanced tutorial)</p>
  
  <p>I am trying to create a form for "extended" user details - eg. company name, street address, etc but the form data is not being saved to the database. </p>
  
  <p>Initially I tried to create a model extending the standard <code>User</code> model, but I gave up on that - too much needed modifying and it was getting into nitty gritty that was way beyond me at this stage.
  So instead I have created a new model called <code>UserProfile</code>: </p>
  
  <pre><code>class UserProfile(models.Model):
      user = models.ForeignKey(settings.AUTH_USER_MODEL, unique=True) 
      company = models.CharField(max_length=50, blank=True)
      address1 = models.CharField(max_length=50, blank=True)
      address2 = models.CharField(max_length=50, blank=True)
      city = models.CharField(max_length=20, blank=True)
      region = models.CharField(max_length=20, blank=True)
      postcode = models.CharField(max_length=10, blank=True)
      country = models.CharField(max_length=20, blank=True)
      phone = models.CharField(max_length=30, blank = True)
  </code></pre>
  
  <p>I have seen different references online as to whether I should link to the User model with a <code>ForeignKey</code> (as above) or with a OneToOne.</p>
  
  <p>I am trying to use a ModelForm (keep it simple for what should be a simple form). Here is my forms.py</p>
  
  <pre><code>from django.forms import ModelForm
  from .models import UserProfile   
  
  class UserDetailsForm(ModelForm):
      class Meta:
          model = UserProfile
          fields = ['company','address1','address2','city','region', 'postcode','country','phone']
  </code></pre>
  
  <p>Here is my view:</p>
  
  <pre><code>def UserDetailsView(request):
      #f = 0
      if request.method == 'POST':
          f = UserDetailsForm(request.POST, instance = request.user)
          if f.is_valid():
              f.save()
      else:
          f = UserDetailsForm(request.POST , instance = request.user)
  
      print "UserDetails objects: ", (UserProfile.objects.all()) 
  
      return render_to_response('plagweb/console/profile.html', 
                                { 'form': f}, 
                                context_instance=RequestContext(request))     
  </code></pre>
  
  <p>(yes there is an inconsistency in UserProfile vs UserDetail - this is a product of all my hacking and will be fixed once I get it working)</p>
  
  <p>Diagnostics show <code>f.is_valid()</code> returning True.
  Similarly, diagnostics show <code>UserProfile.objects.all()</code> as being empty. I tried this in the above view after the save(), and also at the Django console.</p>
  
  <p>Here is my template:</p>
  
  <pre><code> &lt;form method="POST" action=""&gt;
   &lt;table&gt;{{ form }}&lt;/table&gt;
   &lt;input type="submit" value="Update" /&gt;
   {% csrf_token %}
   &lt;/form&gt;
  </code></pre>
  
  <p>At the moment the main problem is that form data is not being saved to the database. I do not know if is being read yet or not (once I have some data in the database...)</p>
  
  <p>One thought is that the <code>User</code> relationship might be causing a problem? </p>
  
  <hr>
  
  <p><em><strong>Addenda, following on from Daniel Roseman's useful comment/help:</em></strong></p>
  
  <p>Now the form is saving correctly (confirmed with diagnostics and command line checks. However when I go back to the form, it is not displaying the existing data. A form with empty data fields is displayed. As far as I can tell, I'm passing the instance data correctly.</p>
  
  <p>Is there a ModelForm setting that needs to change?</p>
  
  <p>Here's the modified View:</p>
  
  <pre><code>def UserDetailsView(request):
      #print "request co:", request.user.profile.company
      f = UserDetailsForm(request.POST, instance = request.user.profile )
  
      if request.method == 'POST':
          if f.is_valid():
              profile = f.save(commit=False)
              profile.user = request.user
              profile.save()
  
      return render_to_response('plagweb/console/profile.html', 
                                { 'form': f}, 
                                context_instance=RequestContext(request))     
  </code></pre>
  
  <p>The diagnostics show that <code>request.user.profile</code> is set correctly (specifically the company field). However the form is displayed with empty fields. The HTML source doesn't show any data values, either.
  As an extra check, I also tried some template diagnostics:</p>
  
  <pre><code>&lt;table border='1'&gt;
      {% for field in form%}
          &lt;tr&gt;
              &lt;td&gt;{{field.label}}&lt;/td&gt;
              &lt;td&gt;{{field.value}}&lt;/td&gt;
          &lt;/tr&gt;
      {% endfor%}
  &lt;/table&gt;
  </code></pre>
  
  <p>This lists the field labels correctly, but the values are all reported as <code>None</code>. </p>
  
  <p>For completeness, the UserProfile's user field is now defined as <code>user = models.OneToOneField(settings.AUTH_USER_MODEL)</code> and the User.profile lambda expression is unchanged.</p>