title: Django - ModelForm Create or Update?
body: <p>I need to have a form that allows the creation or addition of sessions on a planning</p>
  
  <h2>Model</h2>
  
  <pre><code>class Session(models.Model):
      tutor = models.ForeignKey(User)
      start_time = models.DateTimeField()
      end_time = models.DateTimeField()
  </code></pre>
  
  <h2>Form</h2>
  
  <pre><code>class SessionForm(forms.ModelForm):
      class Meta:
          model = Session
          exclude = ['tutor']
  </code></pre>
  
  <h2>View to render the form</h2>
  
  <pre><code>def editor(request):
      if request.method == 'GET':
          if request.GET['id'] != '0':
              # The user has selected a session
              session = Session.objects.get(id=request.GET['id'])
              form = SessionForm(instance=session)
          else:
              # The user wants to add a new session
              form = SessionForm()
          return render_to_response('planner/editor.html',
              {'form': form,}, context_instance=RequestContext(request),)
  </code></pre>
  
  <h2>Template <em>editor.html</em></h2>
  
  <pre><code>&lt;form action="/planner/post" method="post"&gt;{% csrf_token %}
  {{ form.as_p }}
  &lt;/form&gt;
  </code></pre>
  
  <h2>View to post the values</h2>
  
  <pre><code>def post(request):
      if request.method == 'POST':
          form = SessionForm(request.POST)
          if form.is_valid():
              form.instance.tutor = request.user
              form.save()
              obj = {'posted': True}
              return HttpResponse(json.dumps(obj), mimetype='application/json')
          else:
              return render_to_response('planner/editor.html',
                  form, context_instance=RequestContext(request),)
  </code></pre>
  
  <h2>Problem</h2>
  
  <p>Sessions are always created (never updated)</p>
  
  <h2>Questions</h2>
  
  <ul>
  <li>In my view <code>post</code> how do I know that the session must be updated and not created ?</li>
  <li>Is there a way to simplify this code ?</li>
  </ul>