I have a table called items
office_id , sem ,sy ,remarks, recorded_by
And table called customuser
userid, password, email
How can i save the current user's userid here at table items > recorded_by
for now this is my views.py
class Add(LoginRequiredMixin, CreateView):
form_class = CreateForm
model = ClearanceItem
template_name = 'clearance/add.html'
def form_valid(self, form):
form.instance.recorded_by= self.request.user
return super().form_valid(form)
CodePudding user response:
Create the object before committing it to the database, update the recorded_by
attribute, and then commit it to the database.
def form_valid(self, form):
instance = form.save(commit=False)
instance.recorded_by = self.request.user
instance.save()
return HttpResponseRedirect(self.get_success_url())