> For whatever reason, if a field is not provided in the request data, then 
> Django is assuming it is an empty string and overwriting existing data.

I recently discovered that if the model field has the *default* attribute, 
then the non-existent fields in the POST data will not be populated with 
default values, but will remain untouched. Example: 

class User(models.Model):
    first_name = models.CharField(max_length=100, blank=True, default='')
    last_name = models.CharField(max_length=100, blank=True)

class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('first_name', 'last_name')

user = User(first_name='John', last_name='Doe')
form = UserForm({}, instance=user)
instance = form.save()
# -> <User: first_name="John", last_name="">

I'm not sure if this is a bug or a feature, but the documentation does not 
mention this point:

> If an optional field doesn’t appear in the form’s data, the resulting 
> model instance uses the model field default, if there is one, for that 
> field. 

You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
For more options, visit https://groups.google.com/d/optout.

Reply via email to