Ticket #3639: r6635_create_update_newforms.diff

File r6635_create_update_newforms.diff, 4.9 KB (added by brosner, 8 years ago)

updated to trunk. fixed several issues with last patch and added form and formfield_callback overrides.

  • django/views/generic/create_update.py

     
    11from django.core.xheaders import populate_xheaders
    22from django.template import loader
    3 from django import oldforms
    4 from django.db.models import FileField
     3from django import newforms as forms
    54from django.contrib.auth.views import redirect_to_login
    65from django.template import RequestContext
    76from django.http import Http404, HttpResponse, HttpResponseRedirect
     
    109
    1110def create_object(request, model, template_name=None,
    1211        template_loader=loader, extra_context=None, post_save_redirect=None,
    13         login_required=False, follow=None, context_processors=None):
     12        login_required=False, context_processors=None, form=forms.BaseForm,
     13        formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
    1414    """
    1515    Generic object-creation function.
    1616
     
    2323    if login_required and not request.user.is_authenticated():
    2424        return redirect_to_login(request.path)
    2525
    26     manipulator = model.AddManipulator(follow=follow)
    27     if request.POST:
    28         # If data was POSTed, we're trying to create a new object
    29         new_data = request.POST.copy()
     26    ModelForm = forms.form_for_model(model, form=form, formfield_callback=formfield_callback)
     27    if request.method == 'POST':
     28        form = ModelForm(request.POST, request.FILES)
     29       
     30        if form.is_valid():
     31            new_object = form.save()
    3032
    31         if model._meta.has_field_type(FileField):
    32             new_data.update(request.FILES)
    33 
    34         # Check for errors
    35         errors = manipulator.get_validation_errors(new_data)
    36         manipulator.do_html2python(new_data)
    37 
    38         if not errors:
    39             # No errors -- this means we can save the data!
    40             new_object = manipulator.save(new_data)
    41 
    4233            if request.user.is_authenticated():
    4334                request.user.message_set.create(message=ugettext("The %(verbose_name)s was created successfully.") % {"verbose_name": model._meta.verbose_name})
    4435
     
    5142            else:
    5243                raise ImproperlyConfigured("No URL to redirect to from generic create view.")
    5344    else:
    54         # No POST, so we want a brand new form without any data or errors
    55         errors = {}
    56         new_data = manipulator.flatten_data()
     45        form = ModelForm()
    5746
    58     # Create the FormWrapper, template, context, response
    59     form = oldforms.FormWrapper(manipulator, new_data, errors)
     47    # Create the template, context, response
    6048    if not template_name:
    6149        template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower())
    6250    t = template_loader.get_template(template_name)
     
    7361def update_object(request, model, object_id=None, slug=None,
    7462        slug_field='slug', template_name=None, template_loader=loader,
    7563        extra_context=None, post_save_redirect=None,
    76         login_required=False, follow=None, context_processors=None,
    77         template_object_name='object'):
     64        login_required=False, context_processors=None,
     65        template_object_name='object', form=forms.BaseForm,
     66        formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
    7867    """
    7968    Generic object-update function.
    8069
     
    10291    except ObjectDoesNotExist:
    10392        raise Http404, "No %s found for %s" % (model._meta.verbose_name, lookup_kwargs)
    10493
    105     manipulator = model.ChangeManipulator(getattr(object, object._meta.pk.attname), follow=follow)
     94    ModelForm = forms.form_for_instance(object, form=form, formfield_callback=formfield_callback)
    10695
    107     if request.POST:
    108         new_data = request.POST.copy()
    109         if model._meta.has_field_type(FileField):
    110             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)
     96    if request.method == 'POST':
     97        form = ModelForm(request.POST, request.FILES)
     98        if form.is_valid():
     99            object = form.save()
    115100
    116101            if request.user.is_authenticated():
    117102                request.user.message_set.create(message=ugettext("The %(verbose_name)s was updated successfully.") % {"verbose_name": model._meta.verbose_name})
     
    124109            else:
    125110                raise ImproperlyConfigured("No URL to redirect to from generic create view.")
    126111    else:
    127         errors = {}
    128         # This makes sure the form acurate represents the fields of the place.
    129         new_data = manipulator.flatten_data()
     112        form = ModelForm()
    130113
    131     form = oldforms.FormWrapper(manipulator, new_data, errors)
    132114    if not template_name:
    133115        template_name = "%s/%s_form.html" % (model._meta.app_label, model._meta.object_name.lower())
    134116    t = template_loader.get_template(template_name)
Back to Top