Django admin:按URL传递变量 [英] Django admin: pass variable by URL

查看:406
本文介绍了Django admin:按URL传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过URL将变量传递给django管理员中的另一个页面。
它似乎不工作,我想传递变量/?name = hello,并通过request.GET.get [name,]来捕获它,但url变成/? e = 1。
如果我使用默认参数'q',它是有效的,但它会有冲突。
似乎这个问题只是django-admin。我需要通过url不要发布...
有谁知道如何解决这个问题

i want to pass a variable by URL to another page in django admin. it seems it is not working, i want to pass the variable "/?name=hello", and catch it by request.GET.get["name",""].but the url becomes "/?e=1" after it passed. if i use the default parameter'q', it works, but it will have a conflict. it seems this problem is django-admin only. and i need pass it by url not post... does any one knows how to solve this problem

谢谢

推荐答案

问题在于 name 不存在,当 get_query_set 尝试将其解析为模型字段。因此,一个 incorrectLookupParameters()异常被引发,这反过来又重定向到不是很有帮助的e = 1url。 Django 1.4中引入了custom filterspecs,解决了这个问题。在此之前,一个可能的解决方案是动态地覆盖您的 ModelAdmins get_changelist返回的 ChangeList 方法

The problem is that the lookup name does not exist when the get_query_set tries to resolve it as a model field. Thus an IncorrectLookupParameters() exception is raised which in turn redirects to the not very helpful "e=1" url. This problem is solved in Django 1.4 with the introduction of custom filterspecs. Until then one possible solution is to dynamically overwrite the ChangeList class returned by your ModelAdmins get_changelist() method.

此解决方案适用于Django 1.3:

This solution works on Django 1.3:

class MyModelAdmin(ModelAdmin):

    def get_changelist(self, request, **kwargs):
        changelist_class = super(MyModelAdmin, self).get_changelist(request, 
                                                                 **kwargs)

        class CustomChangeList(changelist_class):
            def __init__(self, request, *args, **kwargs):
                self._name = request.GET.get('name')
                super(CustomChangeList, self).__init__(request, *args, **kwargs)

            def get_query_set(self, *args, **kwargs):
                if self._name:
                    del self.params['name']

                qs = super(CustomChangeList, self).get_query_set(*args, **kwargs)

                if self._name:
                    return qs.filter([FILTER WHAT YOU WANT HERE...])

                return qs


        return CustomChangeList

这篇关于Django admin:按URL传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆