如何在Django的ModelForm中使用请求

编程入门 行业动态 更新时间:2024-10-28 02:25:20
本文介绍了如何在Django的ModelForm中使用请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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中使用请求

本文发布于:2023-11-23 09:27:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620935.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Django   ModelForm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!