Django - ModelForm Create or Update? [duplicate]

南笙酒味 提交于 2020-01-09 09:08:14

问题


I need to have a form that allows the creation or addition of sessions on a planning

Model

class Session(models.Model):
    tutor = models.ForeignKey(User)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()

Form

class SessionForm(forms.ModelForm):
    class Meta:
        model = Session
        exclude = ['tutor']

View to render the form

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),)

Template editor.html

<form action="/planner/post" method="post">{% csrf_token %}
{{ form.as_p }}
</form>

View to post the values

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),)

Problem

Sessions are always created (never updated)

Questions

  • In my view post how do I know that the session must be updated and not created ?
  • Is there a way to simplify this code ?

回答1:


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()



回答2:


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))



回答3:


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.




回答4:


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)


来源:https://stackoverflow.com/questions/10205657/django-modelform-create-or-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!