Skip to content

Instantly share code, notes, and snippets.

@davidejones
Created August 27, 2015 01:57
Show Gist options
  • Save davidejones/c11f710a74c0ca8a3964 to your computer and use it in GitHub Desktop.
Save davidejones/c11f710a74c0ca8a3964 to your computer and use it in GitHub Desktop.
partner select on change list
from django.contrib.admin.views.main import ChangeList
# make the partner select pass through without ?e=1
class UserChangeList(ChangeList):
def get_filters_params(self, params=None):
params = super(UserChangeList, self).get_filters_params(params)
if 'userprofile__partners__exact' in params:
del params['userprofile__partners__exact']
return params
@admin.register(User, site=site)
class MyUserAdmin(UserAdmin):
def get_changelist(self, request, **kwargs):
return UserChangeList
def get_queryset(self, request):
qs = super(MyUserAdmin, self).get_queryset(request)
conditions = []
if request.GET.get('userprofile__partners__exact', None):
selected_partner_id = request.GET.get('userprofile__partners__exact', None)
conditions.append(Q(userprofile__partners=int(selected_partner_id[0])))
else:
conditions.append(Q(userprofile__partners=request.session['current_partner_id']))
if request.user:
conditions.append(Q(id=request.user.pk))
from operator import __or__
return qs.filter(reduce(__or__, conditions))
def changelist_view(self, request, extra_context=None):
if extra_context is None:
extra_context = {}
selected_partner_id = request.GET.get('userprofile__partners__exact', None)
if selected_partner_id:
extra_context['selected_partner_id'] = int(selected_partner_id)
changelist_view_response = super(MyUserAdmin, self).changelist_view(request=request, extra_context=extra_context)
return changelist_view_response
@register.inclusion_tag('admin/quick_partner_select.html', takes_context=True)
def quick_partner_select(context):
context['partners'] = Partner.objects.all()
return context
{% extends "admin/change_list.html" %}
{% load i18n admin_urls admin_static admin_list bootstrapped_goodies_tags awesome_admin_tags %}
{% block object-tools %}
{% if request.user|is_vvadmin or request.user.is_superuser %}
{% quick_partner_select %}
{% endif %}
{{ block.super }}
{% endblock %}
{% block content %}
{{ block.super }}
{% endblock %}
<form method="get" name="quick_select_partner_form" action="">
<select onchange="this.form.submit()" name="userprofile__partners__exact" class="form-control" style="width:250px;float:right;margin-left:30px;margin-top:7px;margin-right:10px">
{% for partner in partners %}
<option value="{{ partner.id }}" {% if selected_partner_id == partner.id %}selected="selected"{% endif %}>{{ partner.name }}</option>
{% endfor %}
</select>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment