English 中文(简体)
在视图之外访问已认证的用户。
原标题:Access authenticated user outside of a view
  • 时间:2010-02-05 01:19:59
  •  标签:
  • django

我正在尝试在表单类中访问已验证的用户。我尝试通过从视图传递请求对象到类初始化来实现此功能,但似乎有些不规范。是否有更好的方法可以在视图之外访问已验证的用户或请求对象?

class LicenseForm(forms.Form):
       def __init__(self, *args, **kwargs):
    #self.fields[ license ] = forms.ModelChoiceField(queryset=self.license_queryset(), empty_label= None , widget=forms.RadioSelect())   

    def license_queryset():
        queryset = License.objects.filter(organization__isnull=True)
        # add addtional filters if the logged in user belongs to an organization
        return queryset

    licenses = forms.ModelChoiceField(queryset=license_queryset(), empty_label= None , widget=forms.RadioSelect())
最佳回答

是的,你可以做到,以下是说明:http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser

虽然这个方法有效,但我个人更愿意将用户传递给视图中的表单。 这样感觉不像是一个hack。

你也可以展示你的代码,也许可以改进它。你为什么要在表单中访问用户?

Update: You could do something like this:

class LicenseForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(LicenseForm, self).__init__(*args, **kwargs)
        self._user = kwargs.get( user ,None)
        self.fields[ licenses ] = forms.ModelChoiceField(queryset=self.license_queryset(), empty_label= None , widget=forms.RadioSelect())

    def license_queryset(self):
        queryset = License.objects.filter(organization__isnull=True)
        if self._user and self._user.belongsTo( SomeOrganization ):
            queryset  = queryset.filter(whatever= fits )
        return queryset

在我看来,这种方法比干涉本地线程要干净得多。

问题回答

暂无回答




相关问题
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 ...

热门标签