我有 Django 应用程序和管理页面。
在管理页面内,我有一个模型具有autocomplete_fields
. 我可以对结果进行排序,但是我无法对结果进行排序。
它总是按 pk 而不是我设置的值排序。
@admin.register(Assortment)
class AssortmentAdmin(ImportExportActionModelAdmin):
list_display = ['name']
exclude = ['customers']
# inlines = [ProductAssortmentInLine]
autocomplete_fields = ['products']
@admin.register(Product)
class ProductAdmin(ImportExportActionModelAdmin):
exclude = ['associated_products', 'subsidiary_excluded', 'customers']
list_display = ['product_ref', 'name', 'price', 'group', 'retail_unit', 'active']
list_editable = ['price', 'retail_unit', 'active']
list_filter = ['group']
search_fields = ['product_ref', 'name', 'group']
resource_class = ProductResource
# inlines = [CustomerInLine]
def get_queryset(self, request):
qs = super(ProductAdmin, self).get_queryset(request)
qs = qs.order_by(Cast(F('product_ref'), IntegerField()))
return qs
如何解决这个问题?