从Django中的基于类的通用视图将Request.user对象发送到ModelForm [英] Sending request.user object to ModelForm from class based generic view in Django

查看:76
本文介绍了从Django中的基于类的通用视图将Request.user对象发送到ModelForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的目标是能够在ModelForm中过滤一个ModelChoiceField查询集,只包含request.user创建的Places。



我的ModelForm只是:

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

我希望能够添加以下内容:

  def __init __(self,* args,** kwargs):
super(PlaceEventForm,self).__ init __(* args,** kwargs)
self。字段['place']。queryset = Place.objects.filter(created_by = request.user)

但是,我似乎找不到一种在ModelForm中访问请求的方法。



我的视图是这样的:

  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
/ pre>

但是我收到错误: init ()得到了一个意想不到的关键字参数'place_user'



这个有什么想法?或者任何人都可以考虑在视图中过滤我的ModelChoiceField,而不需要将我的请求传递给ModelForm?

解决方案

你需要从用户 kwargs PlaceEventForm .__ init __()方法,以防止它进入 ModelForm .__ init __()方法:



views.py: / p>

  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})
返回kwargs

forms.py:

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

def __init __(self,* args,** kwargs):
user = kwargs .pop('place_user')
#现在kwargs不包含'place_user',所以我们可以安全地将其传递给基类方法
super(PlaceEventForm,self).__ init __(* args,* * kwargs)
self.fields ['place']。queryset = Place.objects.filter(created_by = user)


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?

解决方案

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)

这篇关于从Django中的基于类的通用视图将Request.user对象发送到ModelForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆