gpt4 book ai didi

Django-admin 按多个字段排序

转载 作者:行者123 更新时间:2023-12-04 05:33:30 25 4
gpt4 key购买 nike

如何按 django-admin 中的多个字段进行排序?

谢谢

最佳答案

尝试这个:

在您的模型中设置排序 Meta :

class Meta:
ordering = ["some_field", "other_field"]

并在 admin.py 中添加此类:
from django.contrib.admin.views.main import ChangeList

class SpecialOrderingChangeList(ChangeList):
"""
Django 1.3 ordering problem workaround
from 1.4 it's enough to use `ordering` variable
"""
def get_query_set(self):
queryset = super(SpecialOrderingChangeList, self).get_query_set()
return queryset.order_by(*self.model._meta.ordering)

在您的 admin.ModelAdmin 中添加此方法
def get_changelist(self, request, **kwargs): 
return SpecialOrderingChangeList

来源: https://groups.google.com/forum/?fromgroups#!topic/django-users/PvjClVVgD-s

关于Django-admin 按多个字段排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8855151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com