[Fixed]-Django – ModelForm Create or Update?

25👍

If you want to update a session, you need to provide the instance when you bind the form.

If the form is valid, you can then save with commit=False, and update the tutor.

form = SessionForm(instance=instance, data=request.POST)
if form.is_valid():
    instance = form.save(commit=False)
    instance.tutor = request.user
    instance.save()

1👍

from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, Http404
from django.template import RequestContext
from application.models import Session
from application.forms import SessionForm

def allInOneView(request):
    session_id = request.POST.get('session_id')

    if session_id:
        session = get_object_or_404(Session, pk=session_id)
    else:
        session = None

    """
    A subclass of ModelForm can accept an existing model instance 
    as the keyword argument instance; 
    if this is supplied, save() will update that instance. 
    If it's not supplied, save() will create a new instance of the specified model.
    """
    form = SessionForm(instance=session)

    if request.method == 'POST':
        form = SessionForm(request.POST, instance=session)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(request.path)

    return render_to_response('planner/editor.html', {
        'form': form
    }, context_instance=RequestContext(request))

1👍

What I usually do now (following the advices mentioned here) is using only one view passing the optional session_id (no session_id for creation) to the URL dispatcher.

<form action="{% url session_edit session_id=session_id|default_if_none:"" %}"
    method="post">{% csrf_token %}
{{ form.as_p }}
</form>
url('^planner/edit$', session_edit, name='session_edit'),
url('^planner/edit/(?P<session_id>\d+)$', session_edit, name='session_edit'),

I find that regrouping all 4 cases

  • Get creation form
  • Get update form
  • Post creation form
  • Post update form

into one view is much more maintainable.

0👍

Do it all in the one view. Something like:

def session_manager(request):

    session = None
    try:
        session = Session.objects.get(id=request.POST['id'])
    except Session.DoesNotExist:
        pass

    if request.method == "POST":
       kwargs = {
            data = request.POST
       }
       if session:
            # Update
            kwargs['instance'] session 
       form = SessionForm(**kwargs)
       if form.is_valid():
            ...
    else:
        form = SessionForm(instance=session)

Leave a comment