如何在Django的ModelForm中使用请求 [英] How to use the request in a ModelForm in Django
问题描述
class BookSubmitForm( ModelForm):
book = forms.ModelChoiceField(queryset = Book.objects.filter(owner = request.user),)
...
Django是否将请求传递给表单?这是好习惯吗?如何使用请求? (当然名字请求没有定义)
编辑:
我尝试过另一种解决方案是调用视图中的表单传递请求:
form = BookSubmitForm(request)
然后以我使用的形式:
class BookSubmitForm(ModelForm):
def __init __(self,request,* args,** kwargs):
super(BookSubmitForm,self).__ init __(* args,** kwargs)
self.fields [library]。queryset = Library.objects.filter(owner = request.user)
它的作品和代码是在形式。现在我不知道这是最好的解决方案,可以改进吗?
不,请求不会传递给的ModelForm。您需要在您的视图中执行此操作:
form = BookSubmitForm()
form.fields [ 'book']。queryset = Book.objects.filter(owner = request.user)
#将表单传递给模板等
正如你所说,将其封装在Form对象中通常更为清晰,特别是如果您有几个需要过滤的查询集的字段。要执行此操作,请覆盖表单的 __ init __()
,并接受请求
的kwarg:
class BookSubmitForm(ModelForm):
def __init __(self,* args,** kwargs):
self.request = kwargs.pop(request)
super(BookSubmitForm,self).__ init __(* args,** kwargs)
self.fields [book]。queryset = Book.objects.filter(owner = self.request.user)
self.fields [whatever]。queryset = WhateverModel.objects.filter(user = self.request.user)
然后只要您在视图中实例化 BookSubmitForm
即可传递请求:
def book_submit(request):
如果request.method ==POST:
form = BookSubmitForm(request.POST,request = request)
#做任何
else:
form = BookSubmitForm(request = request)
#render form,etc
I would like to make a queryset where the current user is used as a filter in a ModelForm:
class BookSubmitForm(ModelForm):
book = forms.ModelChoiceField(queryset=Book.objects.filter(owner=request.user),)
...
Does Django pass the request to the form? Is it good practice? How can I use the request? (of course the name request is not defined)
Edit:
I tried another solution which is to call the form in the view passing it the request:
form = BookSubmitForm(request)
and then in the form I use this:
class BookSubmitForm(ModelForm):
def __init__(self, request, *args, **kwargs):
super(BookSubmitForm, self).__init__(*args, **kwargs)
self.fields["library"].queryset = Library.objects.filter(owner=request.user)
It works and the code is in the form. Now I'm not sure it's the best solution, could it be improved?
No, the request is not passed to the ModelForm. You'll need to do something like this in your view:
form = BookSubmitForm()
form.fields['book'].queryset = Book.objects.filter(owner=request.user)
# pass form to template, etc
As you said, it's often cleaner to encapsulate this in the Form object, particularly if you have several fields that will need filtered querysets. To do this, override the forms's __init__()
and have it accept a kwarg of request
:
class BookSubmitForm(ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request")
super(BookSubmitForm, self).__init__(*args, **kwargs)
self.fields["book"].queryset = Book.objects.filter(owner=self.request.user)
self.fields["whatever"].queryset = WhateverModel.objects.filter(user=self.request.user)
Then just pass request whenever you instantiate BookSubmitForm
in your view:
def book_submit(request):
if request.method == "POST":
form = BookSubmitForm(request.POST, request=request)
# do whatever
else:
form = BookSubmitForm(request=request)
# render form, etc
这篇关于如何在Django的ModelForm中使用请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!