Pass current user to initial for CreateView in Django

后端 未结 1 1323
灰色年华
灰色年华 2021-02-04 22:34

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 c

相关标签:
1条回答
  • 2021-02-04 22:47

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

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(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.

    0 讨论(0)
提交回复
热议问题