Grey
Grey

Reputation: 1385

Django admin: pass variable by 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

thanks

Upvotes: 8

Views: 5413

Answers (3)

kopi22
kopi22

Reputation: 1

Looks like the question is still unanswered and similar to another question. meesterguyperson posted there a great answer, go check it out.

The solution is to create a "fake filter" that will register a URL parameter with django but will not affect the queryset and admin site appearance.

I adapted meesterguyperson's answer a bit myself and instead of using a blank template to hide the "fake filter", I simply exclude it from the filter pane by returning False from has_output method.

from django.contrib.admin.filters import SimpleListFilter

class KwargPassThroughFilter(SimpleListFilter):
title = ''

# hide from filter pane
def has_output(self):
    return False

# these two function below must be implemented for SimpleListFilter to work
# (any implementation that doesn't affect queryset is fine)
def lookups(self, request, model_admin):
    return (request.GET.get(self.parameter_name), ''),

def queryset(self, request, queryset):
    return queryset

Now, you can subclass this filter and override the parameter_name with the desired name of your param, e.g.

class NameKwargPassThroughFilter(KwargPassThroughFilter):
    parameter_name = 'name'

and append register the filter in the ModelAdmin subclass

class SomethingAdmin(admin.ModelAdmin):
    ...

    list_filter = (
        ...,
        NameKwargPassThroughFilter,
        ...,
    )

    ...

At this point, you can freely use the param in the URL and access it within the scope of SomethingAdmin pages...

Works for Django 3.0+

Upvotes: 0

Rune Kaagaard
Rune Kaagaard

Reputation: 6798

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.

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

Upvotes: 12

crodjer
crodjer

Reputation: 13614

Can you explain your problem a bit more....why do you want to pass a variable to django admin... also you cant catch GET variable like this... Either use:

request.GET['name'] 

or

request.GET.get('name','')

.get is a instancemethod not a dictionary.

Upvotes: 1

Related Questions