如何使用自定义用户在Django管理员中继承用户的字段?

时间:2016-01-09 17:16:12

标签: python django django-admin

我想将settings.AUTH_USER_MODEL添加到User模型的管理员中。 我将其注册到我在文档中找到的snippet

class UserAdmin(BaseUserAdmin):
    # The forms to add and change user instances
    form = UserChangeForm
    add_form = UserCreationForm

    # The fields to be used in displaying the User model.
    # These override the definitions on the base UserAdmin
    # that reference specific fields on auth.User.
    list_display = ('email', 'bdate')
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        ('Personal info', {'fields': ('bdate', 'website', 'location')}),
    )
    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2')}
        ),
    )
    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ()

在字段集Personal info中,我添加了所有自定义信息。现在我还要显示所有继承的字段,如first_nameusername等。我可以逐个添加到字段集中,但我不确定这是否正确。

有没有办法从User模型继承它们而不明确指定?

1 个答案:

答案 0 :(得分:2)

您可以使用<script type="text/x-mathjax-config"> MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']], displayMath: [['\\[','\\]'], ['$$','$$']]}}); </script> <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"> </script>

ModelAdmin.get_fieldsets()