Ticket #3639: create_update__newforms.diff

File create_update__newforms.diff, 3.5 KB (added by webograph <webograph@…>, 8 years ago)
  • django/views/generic/create_update.py

     
    11from django.core.xheaders import populate_xheaders
    22from django.template import loader
    3 from django import oldforms
     3from django import newforms as forms
    44from django.db.models import FileField
    55from django.contrib.auth.views import redirect_to_login
    66from django.template import RequestContext
     
    2323    if login_required and not request.user.is_authenticated():
    2424        return redirect_to_login(request.path)
    2525
    26     manipulator = model.AddManipulator(follow=follow)
     26    ModelForm=forms.models.form_from_model(model)
    2727    if request.POST:
    2828        # If data was POSTed, we're trying to create a new object
    2929        new_data = request.POST.copy()
     
    3131        if model._meta.has_field_type(FileField):
    3232            new_data.update(request.FILES)
    3333
    34         # Check for errors
    35         errors = manipulator.get_validation_errors(new_data)
    36         manipulator.do_html2python(new_data)
     34        # Actually read the data
     35        form=ModelForm(new_data)
    3736
    38         if not errors:
     37        if not form.errors:
    3938            # No errors -- this means we can save the data!
    40             new_object = manipulator.save(new_data)
     39            new_object = form.save()
    4140
    4241            if request.user.is_authenticated():
    4342                request.user.message_set.create(message=gettext("The %(verbose_name)s was created successfully.") % {"verbose_name": model._meta.verbose_name})
     
    5352    else:
    5453        # No POST, so we want a brand new form without any data or errors
    5554        errors = {}
    56         new_data = manipulator.flatten_data()
     55        form=ModelForm()
    5756
    58     # Create the FormWrapper, template, context, response
    59     form = oldforms.FormWrapper(manipulator, new_data, errors)
     57    # Create the template, context, response
    6058    if not template_name:
    6159        template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower())
    6260    t = template_loader.get_template(template_name)
     
    102100    except ObjectDoesNotExist:
    103101        raise Http404, "No %s found for %s" % (model._meta.verbose_name, lookup_kwargs)
    104102
    105     manipulator = model.ChangeManipulator(getattr(object, object._meta.pk.attname), follow=follow)
     103    ModelForm = forms.models.form_for_instance(object)
    106104
    107105    if request.POST:
    108106        new_data = request.POST.copy()
    109107        if model._meta.has_field_type(FileField):
    110108            new_data.update(request.FILES)
    111         errors = manipulator.get_validation_errors(new_data)
    112         manipulator.do_html2python(new_data)
    113         if not errors:
    114             object = manipulator.save(new_data)
     109        form=ModelForm(new_data)
     110        if not form.errors:
     111            object = form.save(new_data)
    115112
    116113            if request.user.is_authenticated():
    117114                request.user.message_set.create(message=gettext("The %(verbose_name)s was updated successfully.") % {"verbose_name": model._meta.verbose_name})
     
    126123    else:
    127124        errors = {}
    128125        # This makes sure the form acurate represents the fields of the place.
    129         new_data = manipulator.flatten_data()
     126        form = ModelForm()
    130127
    131     form = oldforms.FormWrapper(manipulator, new_data, errors)
    132128    if not template_name:
    133129        template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower())
    134130    t = template_loader.get_template(template_name)
Back to Top