Thursday, April 27, 2017

Re: Apply default filters in admin

That worked! Thanks!!

Nate

On Wednesday, April 26, 2017 at 6:37:32 AM UTC-5, Todor Velichkov wrote:
I think `ModelAdmin.changelist_view` is the right place.

class MyModelAdmin(admin.ModelAdmin):
   
def changelist_view(request, extra_context=None):
       
if 'lang' not in request.GET:
            q
= request.GET.copy()
            q
.setdefault('lang', 'en')
           
return redirect('%s?%s' % (request.path, q.urlencode()))
       
return super(MyModelAdmin, self).changelist_view(
            request
, extra_context=extra_context,
       
)




On Tuesday, April 25, 2017 at 2:08:33 AM UTC+3, Nate Granatir wrote:
Where would I put that code? Is there a way to do it when the Admin form is initialized?

On Monday, April 24, 2017 at 5:31:18 PM UTC-5, Todor Velichkov wrote:
I think a simple redirect can fix your problem.

Something like this:

if 'lang' not in request.GET:
    q
= request.GET.copy()
    q
.setdefault('lang', 'en')
   
return redirect('%s?%s' % (request.path, q.urlencode()))




On Monday, April 24, 2017 at 7:12:18 PM UTC+3, Nate Granatir wrote:
In my app I have admin filters for a field called "language" - language is a field in several of my models - and I've created them custom, inheriting from SimpleListFilter, so that I can apply a default of English when none has yet been specified. This works almost perfectly - the only problem is that if I do it this way, by altering the queryset in the admin filter when the user has not selected an option, the parameter is not displayed to the user in the GET paramaters of the URL, which I worry is confusing to the user. 

Is there any way to solve this problem? Essentially I want to apply a default admin filter and have that filter appear in the URL as if it were chosen by the user. I am open to alternative solutions if anyone has done something like this before, even if it means a solution other than using a custom admin filter (e.g. some way of applying default GET parameters before the request is passed to the admin page.)

Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e7879f72-f991-4665-b0c7-3672530ebaee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment