On Thu, Jan 12, 2012 at 7:40 PM, Tai Lee <real.hu...@mrmachine.net> wrote: > class Profile(models.Model): > first_name = models.CharField(blank=True, max_length=50) > last_name = models.CharField(blank=True, max_length=50) > address = models.CharField(blank=True, max_length=50) > > class ProfileForm(ModelForm): > class Meta: > model = Profile > > profile = Profile.objects.create(first_name='Tai', last_name='Lee', > address='Sydney') > form = ProfileForm({}, instance=profile) > if form.is_valid(): > form.save() > }}} > > The profile will have no first name, last name or address. The form > will produce no validation errors and will save the updated model to > the database. > > This is very surprising and counter-intuitive to me.
The ultimate solution is: don't use model forms. I never use them for anything, precisely because of things like this, where the framework is trying to do too much behind the scenes. It's too magical for my tastes, and while I understand why we added it to the framework, I see it as a crutch for lazy developers. C'mon, it's not a lot of work to create a "normal" (non-model) form class and pass its cleaned data to a model's save() method. End rant. :-) Thanks for writing out this example, Tai. I disagree and do not see it as counterintuitive. The "ProfileForm({}, instance=profile)" is clearly passing in empty data (the empty dictionary), and it makes sense that Django would see the empty data, then determine that empty data is allowed on the fields (blank=True) and set those fields to empty data. If you want to avoid this, you have two options: don't use "blank=True," or don't use a model form. Adrian -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.