Is there a proper way to modify the form fields before validating the data on a Class Based FormView
. More specifically a CreateView
. Is necessary to use Class Based views 'cause I'm throwing in a bunch of custom mixins:
class Create(HtmxRequiredMixin, CreatedByMixin, HxFormValidationMixin, BaseMixin, CreateView):
template_name = 'administration/form.html'
model = Object
form_class = Form
success_url = reverse_lazy('object-list')
hx_retarget = '#CREATE'
base = 'object'
views_list = ('create',)
I used the CreatedByMixin in the DRF viewset for the api rest. it looks like:
class CreatedByMixin:
def create(self, request, *args, **kwargs):
data = request.data.copy()
if request.user.is_authenticated:
data['created_by'] = request.user.id
serializer = self.get_serializer(data=data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
I want a similar mixin for my CreateView but I'm not sure what is the proper method to override.
Also, the HxFormValidationMixin
already override the form_valid
and form_invalid
methods.
Actual implementation of def get_form_kwargs(self):
in FormMixin
which inherit from ContextMixin
. you can access the request using self.request
and when you have self.request
you can do whatever you want.
def get_form_kwargs(self,*args,**kwargs):
kwargs = super().get_form_kwargs(*args,**kwargs)
if self.request.method in ("POST", "PUT"):
data = kwargs['data'].copy()
#modify data here
kwargs['data'] = data
return kwargs