I have a model, which has an author ForeignKey
, as such:
class Appointment(models.Model):
# ...
author = models.ForeignKey(User)
The following seem slightly simpler. Note that self.request is set in View.as_view
class AppointmentCreateView(CreateView):
model=Appointment
form_class = AppointmentCreateForm
def get_form(self, form_class):
form = super(AppointmentCreateView, self).get_form(form_class)
# the actual modification of the form
form.instance.author = self.request.user
return form