Opened 2 years ago

Last modified 2 years ago

#19258 assigned Bug

ModelAdmin customized queryset with extra option

Reported by: honyczek Owned by: arank
Component: contrib.admin Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If you customize queryset in ModelAdmin:

from app.models import License, Server
from django.contrib import admin

class LicenseAdmin(admin.ModelAdmin):
  def queryset(self, request):
    qs = super(LicenseAdmin, self).queryset(request)
    return qs.extra(select={'usage_count': "SELECT COUNT(id) FROM app_server WHERE license_id=license.id",})

  list_display = ('name','description','product_key', 'usage_count')
  
admin.site.register(License, LicenseAdmin)

and want to use extra field called usage_count, you get an error:

ImproperlyConfigured at /admin/app/license/

LicenseAdmin.list_display[3], 'usage_count' is not a callable or an attribute of 'LicenseAdmin' or found in the model 'License'.

Django Admin interface ignores extra field usage_count from QuerySet, or validates columns by another way, which doesn't include QuerySet changes.

Change History (2)

comment:1 Changed 2 years ago by julien

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 2 years ago by arank

  • Owner changed from nobody to arank
  • Status changed from new to assigned
Note: See TracTickets for help on using tickets.
Back to Top