title: Validate nested form in Django
body: <p>I have a <code>UserProfile</code> model which is linked OneToOne with Django <code>User</code> model. While adding a user through a form, I wanted to have one form for both of those. I found a snippet that does exactly that, and I didn't have any problem with it, until I wanted to validate the <code>username</code>. The form isn't throwing a <code>ValidationError</code>, like I would like it to, but returns an error page with <code>ValueError: The User could not be created because the data didn't validate.</code>.</p>
  
  <p>From what I understand (which obviously could be wrong), the current setup doesn't handle <code>ValidationError</code>s from the nested form.</p>
  
  <p>Is there any way to add that functionality? If not, how should I tackle the issue of having one form handle to models?</p>
  
  <p>Code:
  </p>
  
  <pre><code>class CmUserForm(forms.ModelForm):
     password = forms.CharField(widget=forms.PasswordInput(),
                                required=False,
                                help_text=_("Leave empty if you don't want "
                                            "to change it"))
  
     def clean_password(self):
         data = self.cleaned_data['password']
         if data.strip():
             return make_password(data)
         else:
             # If password field is empty, then don't change it
             return self.instance.password
  
      def clean_username(self):
         username = self.cleaned_data['username']
  
         if get_user_model().objects.filter(username=username).exists():
             raise forms.ValidationError(_('This username is already in use.'))
         return username
  
      class Meta:
          model = get_user_model()
          fields = ('first_name', 'last_name', 'email', 'username', 'password', 'is_staff', 'is_active')
  
  
  class CmUserProfileForm(forms.ModelForm):
      def __init__(self, *args, **kwargs):
          # Take User if updating, None if creating
          try:
              self.user = kwargs['instance'].user
          except AttributeError:
              self.user = None
  
          user_kwargs = kwargs.copy()
          user_kwargs['instance'] = self.user
          self.uf = CmUserForm(*args, **user_kwargs)
  
          super(CmUserProfileForm, self).__init__(*args, **kwargs)
  
          self.fields.update(self.uf.fields)
          self.initial.update(self.uf.initial)
  
      class Meta:
          model = UserProfile
          exclude = ['user']
  
      def save(self, *args, **kwargs):
          # Save User and pass it to UserProfile
          user = self.uf.save(*args, **kwargs)
          self.instance.user = user
          return super().save(*args, **kwargs)
  </code></pre>