5👍
✅
Using a hidden field doesn’t stop the user from editing the value. If you want to set the value in the view, then remove it from fields
in the form:
class CardioRecordForm(forms.ModelForm):
class Meta:
model = CardioRecord
fields = [
'run_dist',
'run_time',
]
Then set the value on the form’s instance in the form_valid
method:
class CardioCreateView(CreateView):
def form_valid(self, form):
form.instance.client = self.request.user
return super(CardioCreateView. self).form_valid(form)
You can then remove your get_initial
method.
Source:stackexchange.com