Ticket #3258: save_instance.patch

File save_instance.patch, 1.6 KB (added by SmileyChris, 8 years ago)
  • django/newforms/models.py

     
    2626    """
    2727    Saves bound Form ``form``'s clean_data into model instance ``instance``.
    2828
    29     Assumes ``form`` has a field for every non-AutoField database field in
    30     ``instance``. If commit=True, then the changes to ``instance`` will be
    31     saved to the database. Returns ``instance``.
     29    If commit=True, then the changes to ``instance`` will be saved to the
     30    database. Returns ``instance``.
    3231    """
    3332    from django.db import models
    3433    opts = instance.__class__._meta
     
    3635        raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name)
    3736    clean_data = form.clean_data
    3837    for f in opts.fields:
    39         if not f.editable or isinstance(f, models.AutoField):
     38        if is_instance(f, models.AutoField):
    4039            continue
    41         setattr(instance, f.name, clean_data[f.name])
     40        if clean_data.has_key(f.name):
     41            setattr(instance, f.name, clean_data[f.name])
    4242    if commit:
    4343        instance.save()
    4444        for f in opts.many_to_many:
    45             setattr(instance, f.attname, clean_data[f.name])
     45            if clean_data.has_key(f.name):
     46                setattr(instance, f.attname, clean_data[f.name])
    4647    # GOTCHA: If many-to-many data is given and commit=False, the many-to-many
    4748    # data will be lost. This happens because a many-to-many options cannot be
    4849    # set on an object until after it's saved. Maybe we should raise an
Back to Top