This is getting into django-users territory, but I wanted to point out that
it's often better to leave out the field instead of using a hidden input.
You can then override the form_valid() method, and set the value before the
form is saved.
class PatientCreate(LoginRequiredMixin, UserOrgReq
Ah, I see.
It looks like I can use modelform_factory. So:
class PatientCreate(LoginRequiredMixin, UserOrgRequiredMixin, CreateView):
model = models.Patient
fields = ['name', 'caregiver_name', 'sex', 'birth_date',
'residence', 'country']
becomes
from django.forms.models imp
I'd be pretty sceptical about any extra API here. Even an extra hook seems
a bit much.
def get_form_class(self):
base_form = super().get_form_class()
return modelform_factory(self.model, base_form, widgets=...)
Job done, no?
(This assuming that "Just declare your form class normally"
I agree with Tim that this is a slippery slope.
Maybe that adding a ModelFormMixin.get_form_class_options() that returns a
{'fields': self.fields} dict by default and is passed to
modelform_factory(**kwargs)
in get_form_class() would be a good compromise?
Best,
Simon
Le mardi 4 décembre 2018 1
What I meant is that modelform_factory() also has these parameters:
localized_fields is a list of names of fields which should be localized.
labels is a dictionary of model field names mapped to a label.
help_texts is a dictionary of model field names mapped to a help text.
error_messages is a
https://code.djangoproject.com/ticket/24589
This ticket has a patch to pass through the "widgets" argument for generic
class-based views.
It was closed with "won't fix" with this comment: "*I don't think the
possibility of saving a few lines of user code justifies the complexity of
reimplement