English 中文(简体)
如何在Django的模拟表格中使用这一请求
原标题:How to use the request in a ModelForm in Django
  • 时间:2012-01-12 20:02:53
  •  标签:
  • django
  • forms

我想提出一个问题,即目前的用户在示范表格中被用作过滤器:

class BookSubmitForm(ModelForm):
    book = forms.ModelChoiceField(queryset=Book.objects.filter(owner=request.user),)
...

Django是否将申请通过表格? 这种做法是否好? 我如何利用请求? (当然,姓名要求没有界定)

Edit:

我尝试了另一种解决办法,即把形式称作通过请求:

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)

该守则是行之有效的,其形式是。 现在我不敢肯定它会找到最佳解决办法,能否改进?

最佳回答

否,请求不通过《示范公约》。 你认为有必要做这样的事情:

form = BookSubmitForm()
form.fields[ book ].queryset = Book.objects.filter(owner=request.user)
# pass form to template, etc

正如你所说的那样,在表格标本中概括这一点往往比较清洁,特别是如果你有几个需要过滤的田地。 为此,必须填写以下表格:init__(),并接受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)

在你看来,只要你即刻通过<代码>Book SubmitForm,你就认为:

def book_submit(request):
    if request.method == "POST":
        form = BookSubmitForm(request.POST, request=request)
        # do whatever
    else:
        form = BookSubmitForm(request=request)
    # render form, etc
问题回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签