Sending request.user object to ModelForm from class based generic view in Django

[亡魂溺海] 提交于 2019-12-28 05:33:10

问题


So, my goal is to be able to filter a ModelChoiceField queryset in my ModelForm to only include Places that request.user has created.

My ModelForm is simply:

class PlaceEventForm(models.ModelForm):
    class Meta:
        model = Event

I'd like to be able to add something like:

def __init__(self, *args, **kwargs):
    super(PlaceEventForm, self).__init__(*args, **kwargs)
    self.fields['place'].queryset = Place.objects.filter(created_by=request.user)

However, I can't seem to find a way to access the request in the ModelForm.

My View is like so:

class PlaceEventFormView(CreateView):
    form_class = PlaceEventForm
    template_name = 'events/event_create.html'

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

I'm not sure if this is even close to what I should do, but I tried:

def get_form_kwargs(self):
    kwargs = super(PlaceEventFormView, self).get_form_kwargs()
    kwargs.update({'place_user': self.request.user})
    return kwargs

But I got the error: init() got an unexpected keyword argument 'place_user'

Any ideas on this one? Or can anyone think of a way to filter my ModelChoiceField in the view without needing to pass my request to the ModelForm?


回答1:


You need to pop key user from kwargs in PlaceEventForm.__init__() method, to prevent it from going to ModelForm.__init__() method:

views.py:

class PlaceEventFormView(CreateView):
    form_class = PlaceEventForm
    template_name = 'events/event_create.html'

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

    def get_form_kwargs(self):
        kwargs = super(PlaceEventFormView, self).get_form_kwargs()
        kwargs.update({'place_user': self.request.user})
        return kwargs

forms.py:

class PlaceEventForm(models.ModelForm):
    class Meta:
        model = Event

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('place_user')
        # now kwargs doesn't contain 'place_user', so we can safely pass it to the base class method
        super(PlaceEventForm, self).__init__(*args, **kwargs)
        self.fields['place'].queryset = Place.objects.filter(created_by=user)



回答2:


I'm on an iPhone, but do this:

def get_form(self, form_class):
     form = super(MyView, self).get_form(form_class)
     form.fields['place'].querset = Place....
     return form

Wow that was hard! No indention support!




回答3:


To update Yuji's answer for Django 1.10+ (including Django 2.0+), see the example below (note the updated method signature). Yuji's suggested approach keeps the queryset in the view along with the other business logic, and helps keep any form class extending models.ModelForm clean and straightforward.

def get_form(self, form_class=None):
    if form_class is None:
        form_class = self.get_form_class()
    form = super(MyView, self).get_form()
    form.fields['place'].queryset = Place.objects.filter(created_by=self.request.user)
    return form

Shorter:

def get_form(self, form_class=None):
    form = super(MyView, self).get_form(form_class)
    form.fields['place'].queryset = Place.objects.filter(created_by=self.request.user)
    return form


来源:https://stackoverflow.com/questions/5806224/sending-request-user-object-to-modelform-from-class-based-generic-view-in-django

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