Skip to content

Commit

Permalink
Sort ModelAdmin FK fields alphabetically
Browse files Browse the repository at this point in the history
for the for Component, Product and Version fields.
Fixes kiwitcms#633
  • Loading branch information
ivo0126 committed Jan 16, 2019
1 parent f19445d commit 5b3d3fd
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tcms/management/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from django.contrib import admin
from django.contrib.auth import get_user_model

from tcms.management.models import Classification
from tcms.management.models import Component, Version
Expand All @@ -19,6 +20,12 @@ class ProductsAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'classification', 'description')
list_filter = ('id', 'name', 'classification')

def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "classification":
kwargs["queryset"] = Classification.objects.order_by('name')

return super().formfield_for_foreignkey(db_field, request, **kwargs)


class PriorityAdmin(admin.ModelAdmin):
search_fields = ('value', 'id')
Expand All @@ -34,12 +41,27 @@ class ComponentAdmin(admin.ModelAdmin):
def get_queryset(self, request):
return super().get_queryset(request).select_related('product', 'initial_owner')

def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "product":
kwargs["queryset"] = Product.objects.order_by('name')

if db_field.name == "initial_owner" or db_field.name == "initial_qa_contact":
kwargs["queryset"] = get_user_model().objects.order_by('username')

return super().formfield_for_foreignkey(db_field, request, **kwargs)


class VersionAdmin(admin.ModelAdmin):
search_fields = ('value', 'id')
list_display = ('id', 'product', 'value')
list_filter = ('product',)

def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "product":
kwargs["queryset"] = Product.objects.order_by('name')

return super().formfield_for_foreignkey(db_field, request, **kwargs)


class BuildAdmin(admin.ModelAdmin):
search_fields = ('name', 'build_id')
Expand Down

0 comments on commit 5b3d3fd

Please sign in to comment.