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, etcAs 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中使用请求
发布评论