Accessing request.user in class based generic view CreateView in order to set FK field in Django

十年热恋 提交于 2019-11-27 11:41:32

How about overriding form_valid which does the form saving? Save it yourself, do whatever you want to it, then do the redirect.

class PlaceFormView(CreateView):
    form_class = PlaceForm

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(PlaceFormView, self).dispatch(*args, **kwargs)

    def form_valid(self, form):
        obj = form.save(commit=False)
        obj.created_by = self.request.user
        obj.save()        
        return http.HttpResponseRedirect(self.get_success_url())

I know that this is old, but for other people with this problem:

There is an even simpler way - since saving a form multiple times will always use the same model instance, you can also do:

def form_valid(self, form):
    obj = form.save(commit=False)
    obj.created_by = self.request.user
    return super(PlaceFormView, self).form_valid(form)

That way, you get all the benefits of the super call - it's trivial to see that you're really only adding those two lines of code, and you don't have to repeat yourself by replicating the redirect logic.

An alternate way to do this is to pass the user through overwriting the get_initial() method in the CreateView, and modify save method in the PlaceForm class to save the user:

class PlaceForm(forms.ModelForm):
    ...
    ...
    ...

    def __init__(self, *args, **kwargs):
        self.created_by = kwargs['initial']['created_by']
        super(PlaceForm, self).__init__(*args, **kwargs)

    def save(self, commit=True):
        obj = super(PlaceForm, self).save(False)
        obj.created_by = self.created_by
        commit and obj.save()
        return obj

class PlaceFormView(CreateView):
    ...
    ...
    form_class = PlaceForm

    def get_initial(self):
        self.initial.update({ 'created_by': self.request.user })
        return self.initial

This way the saving logic is still encapsulated within the form class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!