在django auth中重新排序用户 [英] Reorder users in django auth

查看:102
本文介绍了在django auth中重新排序用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,在 django.contrib.auth 中的内置用户模型中有一个ForeignKey,我感到沮丧的是,管理员总是按用户的主键进行排序。



我更喜欢按字母顺序排列用户名,而我的本能就是不想弄脏内脏Django,我似乎找不到一种更简单的方法来重新排序用户。



我可以想到的最简单的方法是将其放入我的Django安装并添加

  order =('username',)

到用户模型的Meta类。



是否有某种monkeypatching,我可以做或任何其他较少入侵修改用户模型的顺序的方式?



另外,任何可以通过进行此更改的任何可能破坏的事情?

解决方案

有一种方法使用 ModelAdmin对象来指定您自己的表单。通过指定您自己的表单,您可以完全控制表单的组成和验证。



说具有FK到用户 Foo



您的 myapp / models.py 可能如下所示:

 从django.db导入模型
from django.contrib.auth .models import User

class Foo(models.Model):
user = models.ForeignKey(User)
some_val = models.IntegerField()

然后,您将创建一个包含此类内容的 myapp / admin.py 从django导入用户
从django导入表单






$ b contrib import admin

class FooAdminForm(forms.ModelForm):
user = forms.ModelChoiceField(queryset = User.objects.order_by('username'))

类Meta:
model = Foo

class FooAdmin(admin.ModelAdmin):
fo rm = FooAdminForm

admin.site.register(Foo,FooAdmin)

一旦你这样做,< select> 下拉菜单将根据用户名对用户对象进行排序。无需担心 Foo 的其他字段...您只需要在 FooAdminForm 类中指定覆盖。不幸的是,您需要为每个想要在管理员站点中呈现的用户提供FK的模型提供此自定义表单定义。


I have a model that has a ForeignKey to the built-in user model in django.contrib.auth and I'm frustrated by the fact the select box in the admin always sorts by the user's primary key.

I'd much rather have it sort by username alphabetically, and while it's my instinct not to want to fiddle with the innards of Django, I can't seem to find a simpler way to reorder the users.

The most straightforward way I can think of would be to dip into my Django install and add

ordering = ('username',)

to the Meta class of the User model.

Is there some kind of monkeypatching that I could do or any other less invasive way to modify the ordering of the User model?

Alternatively, can anyone thing of anything that could break by making this change?

解决方案

There is a way using ModelAdmin objects to specify your own form. By specifying your own form, you have complete control over the form's composition and validation.

Say that the model which has an FK to User is Foo.

Your myapp/models.py might look like this:

from django.db import models
from django.contrib.auth.models import User

class Foo(models.Model):
    user = models.ForeignKey(User)
    some_val = models.IntegerField()

You would then create a myapp/admin.py file containing something like this:

from django.contrib.auth.models import User
from django import forms
from django.contrib import admin

class FooAdminForm(forms.ModelForm):
    user = forms.ModelChoiceField(queryset=User.objects.order_by('username'))

    class Meta:
        model = Foo

 class FooAdmin(admin.ModelAdmin):
     form = FooAdminForm

 admin.site.register(Foo, FooAdmin)

Once you've done this, the <select> dropdown will order the user objects according to username. No need to worry about to other fields on Foo... you only need to specify the overrides in your FooAdminForm class. Unfortunately, you'll need to provide this custom form definition for every model having an FK to User that you wish to present in the admin site.

这篇关于在django auth中重新排序用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆