简体   繁体   English

如何在 Django admin 中按字母顺序排列下拉列表?

[英]How to alphabetically order a drop-down list in Django admin?

Here's a (very) simplified version of my models:这是我的模型的(非常)简化版本:

laboratory/models.py实验室/模型.py

class Lab(Model):
    professor = ForeignKey('authors.Author')

authors/models.py作者/模型.py

class Author(Model):
    name = CharField(max_length=100)

In the Django admin, when I add or update a Lab, a drop-down list containing each professors is automatically generated and displayed.在 Django 管理员中,当我添加或更新实验室时,会自动生成并显示包含每个教授的下拉列表。 The problem is this list is very long and it is not alphabetically ordered.问题是这个列表很长,而且不是按字母顺序排列的。 I want the professor drop-down list to be alphabetically ordered by "name" field.我希望教授下拉列表按“姓名”字段的字母顺序排列。

How can I do that?我怎样才能做到这一点?

You can define default ordering for Author model:您可以为 Author 模型定义默认排序:

class Author(Model):
    name = CharField(max_length=100)

    class Meta:
        ordering = ('name',)

Keep in mind that this causes the objects in Django also to ordered and migration will have to be done.请记住,这会导致 Django 中的对象也是有序的,并且必须完成迁移。

You can do ordering = ['name'] under the AuthorAdmin file to order only for admin dashboard.您可以在 AuthorAdmin 文件下执行ordering = ['name']以仅订购管理仪表板。

ModelAdmin specific ordering via formfield_for_foreignkey 通过 formfield_for_foreignkey 进行 ModelAdmin 特定排序

class MyModelAdmin(admin.ModelAdmin):
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "author":
            kwargs["queryset"] = Author.objects.filter(anyfilters=anyfilters).order_by('name')
        return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

Note IMHO its better not to set the ordering on model because the ordering of your admin page needs to be decoupled from the model.注意恕我直言,最好不要在model上设置排序,因为管理页面的排序需要与模型分离。

Also all the queries fired on the model will use the order_by column, in which case you might have to index the ordering column with your other columns.此外,在模型上触发的所有查询都将使用order_by列,在这种情况下,您可能必须将排序列与其他列建立索引。

The current way to do this (January 2019):当前执行此操作的方法(2019 年 1 月):

In your admin.py file:在您的 admin.py 文件中:

class AuthorAdmin(admin.ModelAdmin):
    ordering = ['name']

And then register it:然后注册它:

admin.site.register(Author, AuthorAdmin)

As described in the docs: https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.ordering如文档中所述: https : //docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.ordering

Well, you can use the ordering variable in django admin, or you could use order_with_respect_to underneath class Meta .好吧,您可以在 django admin 中使用ordering变量,或者您可以class Meta使用order_with_respect_to So, first you need to add an admin.py file to the same directory as your models.py file.因此,首先需要将admin.py文件添加到与models.py文件相同的目录中。 This is what your admin.py file should look like:这是您的admin.py文件的样子:

from django.contrib import admin

from models import Lab

class LabAdmin(admin.ModelAdmin):
    fields = ('name',)

    class Meta:
        ordering = ['name'] # ['-name'] if you want the opposite ordering


admin.site.register(Lab, LabAdmin)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM