2

I am using CreateWithInlinesView from django-extra-views. How would I go about retrieving the current user in this case?

I have something like this currently

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)

and this still returns the error of (1048, "Column 'created_by_id' cannot be null").

edit: made my edit into an answer

4

1 回答 1

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

This is because CreateWithInlinesView subclasses a BaseCreateWithInlinesView with the forms_valid() method that calls form_valid() on the form itself and its inlines.

于 2013-10-24T14:43:37.847 回答