#34566: ModelAdmin get_field_queryset uses related admin ordering, but not
related
admin querysets.
---------------------------------+--------------------------------------
Reporter: Sodium-Hydrogen | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Comment (by Natalia Bidart):
Actually I have managed to reproduce this issue. For the readers, these
are minimal `models.py` and `admin.py`:
* models.py:
{{{
from django.db import models
from django.contrib.auth import get_user_model
User = get_user_model()
class Report(models.Model):
title = models.TextField()
owner = models.ForeignKey(User, on_delete=models.CASCADE)
}}}
* admin.py:
{{{
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.db import models
from .models import Report
User = get_user_model()
class UserLocalAdmin(UserAdmin):
list_display = ("username", "email", "custom_perm_count")
ordering = ["-user_permissions__count"]
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.annotate(models.Count("user_permissions"))
def custom_perm_count(user):
return user.user_permissions__count
custom_perm_count.admin_order_field = "user_permissions__count"
custom_perm_count.short_description = "Perm Count"
class ReportAdmin(admin.ModelAdmin):
filter_fields = ['title']
admin.site.unregister(User)
admin.site.register(User, UserLocalAdmin)
admin.site.register(Report, ReportAdmin)
}}}
Visiting the Report admin detail page (either for add or modify), will
raise:
{{{
File "/home/nessita/fellowship/django/django/db/models/sql/query.py",
line 1681, in names_to_path
raise FieldError(
django.core.exceptions.FieldError: Cannot resolve keyword 'count' into
field. Choices are: codename, content_type, content_type_id, group, id,
name, user
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34566#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018820e4be0a-48e55949-bef2-4dbc-ba88-abcfb6e9df77-000000%40eu-central-1.amazonses.com.