Skip to content Skip to sidebar Skip to footer

Pass Current User To Initial For Createview In Django

In my project I have User model from standard auth Django model and the Todo model. User can have many Todo. When signed in user creates a todo he must be assigned to the todo auto

Solution 1:

You need to use the form_valid method instead of get_initial. Something like this:

defform_valid(self, form):
    form.instance.user = self.request.user
    returnsuper(TodoView, self).form_valid(form)

I have never used get_initial, but I guess it doesn't do anything because you don't include the user field. That wouldn't be useful anyway, because you don't want the user to be able to change the value.

Post a Comment for "Pass Current User To Initial For Createview In Django"