#21753: allow generic editing views to use `form_class` and `fields` together
-------------------------+-------------------------------------------------
     Reporter:           |      Owner:  gabejackson
  gabejackson            |     Status:  new
         Type:  New      |    Version:  master
  feature                |   Keywords:  generic edit views form_class
    Component:  Forms    |  fields ModelForm
     Severity:  Normal   |  Has patch:  1
 Triage Stage:           |      UI/UX:  0
  Unreviewed             |
Easy pickings:  0        |
-------------------------+-------------------------------------------------
 The addition of the `fields` property to ModelFormMixin for generic form
 views allows to set fields for the given `model` as documented here:
 https://docs.djangoproject.com/en/1.6/ref/class-based-views/mixins-
 editing/#django.views.generic.edit.ModelFormMixin.fields

 This allows the following:
 {{{
 class Author(models.Model):
     name = models.CharField(max_length=60)
     url = models.CharField(max_length=60)

 class AuthorUpdate(UpdateView):
     model = Author
     fields = ['name']
 }}}
 Which will result in a form with only the field `name`.

 Unfortunately, it is currently impossible to use the `fields` in
 combination with the `form_class` attribute. This means you cannot
 override fields with custom ModelForms:
 {{{
 class Author(models.Model):
     name = models.CharField(max_length=60)
     url = models.CharField(max_length=60)

 class AuthorForm(ModelForm):
     # Customization ...
     class Meta:
         model = Author

 class AuthorUpdate(UpdateView):
     model = Author
     form_class = AuthorForm
     fields = ['name']
 }}}
 This will result in a form with all fields of Author.

 This is due to the implementation of get_form_class doing this:
 {{{
 def get_form_class(self):
     """
     Returns the form class to use in this view.
     """
     if self.form_class:
         return self.form_class
     else:
         if self.model is not None:
             # If a model has been explicitly provided, use it
             model = self.model
         elif hasattr(self, 'object') and self.object is not None:
             # If this view is operating on a single object, use
             # the class of that object
             model = self.object.__class__
         else:
             # Try to get a queryset and extract the model class
             # from that
             model = self.get_queryset().model

         if self.fields is None:
             warnings.warn("Using ModelFormMixin (base class of %s) without
 "
                           "the 'fields' attribute is deprecated." %
 self.__class__.__name__,
                           PendingDeprecationWarning)

         return model_forms.modelform_factory(model, fields=self.fields)
 }}}
 It is apparent that `fields` gets ignored if `form_class` is set.

 I attached a patch including a test case and fix for this issue/feature.

 This could be classified as a bug, since no warning is raised when using
 `form_class` combined with `fields`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21753>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/054.0f03ea1993d4a332ed65d645f16e8d70%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to