Мы можем создать подкласс CreateView
для учета виджетов:
class WidgetCreateView(CreateView):
<b>widgets = {}</b>
def get_form_class(self):
"""Return the form class to use in this view."""
if self.fields is not None and self.form_class:
raise ImproperlyConfigured(
"Specifying both 'fields' and 'form_class' is not permitted."
)
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 getattr(self, 'object', None) 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:
raise ImproperlyConfigured(
"Using ModelFormMixin (base class of %s) without "
"the 'fields' attribute is prohibited." % self.__class__.__name__
)
return model_forms.modelform_factory(
model,
fields=self.fields,
<b>widgets=self.widgets</b>
)
, тогда мы можем создать подкласс для:
class ObjectCreateView(<b>WidgetCreateView</b>):
model = Object
fields = ['title','content','rating']
widgets = {
<b>'rating': forms.HiddenInput</b>
}