> Since these views "will automatically create a ModelForm,", is it still
possible to specify a widget to use with specific fields in that form?
>
> As in a 'manual' ModelFrom, (
https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/#overriding-the-default-fields
)
> one can specify a widget, overriding the default.
>

I'm a bit confused by your question given that the answer is the exact
section in the aforementioned link.

Are you asking whether or not field attributes in a ModelForm can be
altered via some magic within the CBV without manually specifying/defining
a ModelForm class? If that's the case, then the answer is no as far as I
know.

TL;DR; The view is for processing data and requests, forms are the data
pathway and filter between the end-user (UI) and views. In general, views
should not be concerned about how data is displayed.

The reason for this is to avoid mixing back-end/internal operations and UI
design (in larger applications these can be controlled by different
development teams). Django provides widget defaults and automatic
ModelForms as a convenience to get things displaying quickly in templates
and probably the admin portal, but many applications likely define custom
Form or ModelForm classes anyway to provide other behaviors like custom
validation.

The only exception I can think of would be to subclass an existing model
field, override the default widget, and then use your new field in your
model definition. Heck, doing that you could probably add in functionality
to accept an extra keyword like widget= in the model field definition
pretty easily to keep the subclass more general. I would only go that route
if you have similar field/widget requirements for multiple models, though.
Keep in mind that it will forcibly tie together your data structure and
your UI, which usually causes more problems down the line. Then again, this
whole paragraph is about modifying the model fields, not a change in the
view, so apologies for going OT.

https://docs.djangoproject.com/en/1.9/howto/custom-model-fields/#specifying-the-form-field-for-a-model-field

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" 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].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVDR_PKgREs%2B3asSLJ0xX1HAcRx2NwC-_5D_jFkfy_5tw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to