0👍
✅
You can use Python’s kwargs expansion using **
to do this:
enquiry = Enquiry(**kwargs)
But don’t. This is unsafe for the same reason Rails introduced strong_params. Use a form instead.
1👍
The correct and the easiest way is to use model forms.
class EnquiryForm(ModelForm):
class Meta:
model = Enquiry
fields = ['name', 'email', 'enquiry_input', 'enquiry_type']
You don’t have to display it to the user, you may just use it for validation and saving:
def my_view(request):
form = EnquiryForm(request.POST)
if form.is_valid():
form.save()
return # normally a redirect response here
else:
# react to incorrect data
# you may investigate form.errors
return # some error response
Source:stackexchange.com