Django多对多关系管理员的额外字段以相反的顺序添加

时间:2016-06-23 10:29:32

标签: django django-admin many-to-many

与多个字段的多对多关系的默认管理界面如下图所示 enter image description here

是否可以定义此列表中的项目以相反的顺序显示,以便新字段始终位于第一行的顶部?

1 个答案:

答案 0 :(得分:0)

在django / contrib / admin / options.py

行号约265: 找到这个方法:

def formfield_for_manytomany(self, db_field, request=None, **kwargs):
    """
    Get a form Field for a ManyToManyField.
    """
    # If it uses an intermediary model that isn't auto created, don't show
    # a field in admin.
    if not db_field.rel.through._meta.auto_created:
        return None
    db = kwargs.get('using')

    if db_field.name in self.raw_id_fields:
        kwargs['widget'] = widgets.ManyToManyRawIdWidget(db_field.rel,
                                self.admin_site, using=db)
        kwargs['help_text'] = ''
    elif db_field.name in (list(self.filter_vertical) + list(self.filter_horizontal)):
        kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))

    if 'queryset' not in kwargs:
        queryset = self.get_field_queryset(db, db_field, request)
        if queryset is not None:
            kwargs['queryset'] = queryset

    return db_field.formfield(**kwargs)

尝试操纵/重新排序返回值或尝试查看** kwargs值