django-extra-views获得当前用户

时间:2013-10-24 08:57:32

标签: python django django-forms

我正在使用CreateWithInlinesView中的django-extra-views。在这种情况下,我将如何检索当前用户?

我现在有类似的东西

class PublisherCreateView(CreateWithInlinesView):
    model = Publisher
    inlines = [BookInline,]

    def form_valid(self, form, inlines):
        form.instance.created_by = self.request.user
        return super(PublisherCreateView, self).form_valid(form, inlines)

,这仍然会返回(1048, "Column 'created_by_id' cannot be null")的错误。

编辑:将我的编辑转换为答案

1 个答案:

答案 0 :(得分:2)

    def forms_valid(self, form, inlines):
        form.instance.created_by = self.request.user
        return super(PublisherCreateView, self).forms_valid(form, inlines)

这是因为CreateWithInlinesView使用BaseCreateWithInlinesView方法将forms_valid()作为子类,在方法本身及其内联上调用form_valid()