Я пытаюсь добавить пользовательский порядок в Django admin change_list. Я сделал настраиваемые поля в django admin, и упорядочение не является простым, так как эти поля сделаны специально и не доступны напрямую из класса Django models.py '' 'abc (BaseParamsAdmin): # change_list_template =' admin / logistics_admin / agent_performance_today_changelist.html '
list_display = ['field_engineer', 'leads_assigned','leads_rescedule', 'leads_complete',
'leads_failed', 'current_lead',
'time_spent', 'reassign', 'route']
list_display_links = None
list_filter = [ManagerListFilter, RegionAutoFilter]
ordering = ('_field_engineer',)
search_fields = ['name']
def changelist_view(self, request, extra_context=None):
extra_context = extra_context or {}
response = super().changelist_view(request, extra_context)
_cl = response.context_data['cl']
# additional data on the basis of fes on page
qs = _cl.result_list._clone()
self.get_fe_stats(qs)
extra_context['title'] = 'Control Panel'
return response
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = self._filter_base(request, qs)
qs = self.orders_complete.annotate(_leads_completed=Count(order_status_id__in=Orders.STATUS_COMPLETE))
qs = qs.only("id","username","name","role_id","role__id")
return qs
def _filter_base(self, request, qs):
is_superuser = request.user.is_superuser
if not is_superuser:
logistic_user = CustomUserAuthMapping.get_authenticated_user(request.user.id)
if not logistic_user:
raise Exception('No mapping found for this Manager..')
if logistic_user.user.role.id == LogisticUtils.ROLE_SR_MANAGER:
region_ids = UserRegion.get_users_for_region_manger_distinct(logistic_user.user.id,LogisticUtils.ROLE_SR_MANAGER)
qs = qs.filter(user_region__region_id__in=region_ids,user_region__is_active=1).distinct()
else:
qs = qs.filter(manager=logistic_user.user)
return qs
def field_engineer(self, obj):
#return obj.name
# url_red = reverse('admin:fe_performance', args=(obj.id,))
# return format_html('<a href="{}">{}</a> ', url_red, obj.name)
url_red = ("%s?user=%s&created_at__gte=%s&created_at__lte=%s") % (reverse('admin:logistics_admin_ordershistoryfb_changelist'),
obj.id,LogisticUtils.subtractDateOnly(30),LogisticUtils.getDateOnly())
return format_html('<a href="{}">{}</a> ', url_red, obj.name)
field_engineer.admin_order_field = '_field_engineer'
def leads_assigned(self, obj):
#return self.orders_assign.get(obj.id, 0)
order_assign = OrdersHistory.objects.filter(assign_at__date=LogisticUtils.getDateOnly(), user_id=obj.id,
order_status_id=Orders.STATUS_ASSIGNED).order_by('-id').only("id")
return len(order_assign)
leads_assigned.admin_order_field = '_leads_assigned'
def leads_complete(self, obj):
return self.orders_complete.get(obj.id, 0)
def leads_failed(self, obj):
return self.orders_cancel.get(obj.id, 0)
def on_time_reach(self, obj):
return self.orders_time_reach.get(obj.id, 0)
'' '
Я пробовал' '' field_engineer.admin_order_field = '_field_engineer' '', но, похоже, это не работает.