I\'m writing my first app in Django, and I have a problem with the create_object
Generic View; In my urls.py
, I have:
(r\'^new$\',
You can do this by overriding the get_form
method:
from django.views.generic import CreateView
class CustomCreateView(CreateView):
def get_form(self, form_class):
form = super(CustomCreateView, self).get_form(form_class)
form.instance.user = self.request.user
return form
EDIT: Nowadays I would override form_valid as per Issac Kelly's answer:
from django.views.generic import CreateView
class CustomCreateView(CreateView):
def form_valid(self, form):
form.instance.user = self.request.user
return super(CustomCreateView, self).form_valid(form)
You want to override the form_valid method.