如何在Django中将多个ModelAdmin附加到UserAdmin? [英] how to attach multiple ModelAdmins to UserAdmin in Django?

查看:132
本文介绍了如何在Django中将多个ModelAdmin附加到UserAdmin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索模型,其中 ForeignKey 用户

 类搜索(models.Model):
user = models.ForeignKey(User)
...

我有一个 UserProfile 模型,它有一个 OnetoOne 的关系用户



$ _ code> class UserProfile(models.Model):
user = models.OneToOneField(User)
photo = models.ImageField(upload_to ='profile_images',blank =真的)
ispublic = models.NullBooleanField()

我已附加 admin.py 中的UserProfile 如下:

  class UserProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False

class UserProfileAdmin(UserAdmin):

inlines = UserProfileInline,)
list_filter = UserAdmin.list_filter +('email',)
list_displa y =('username','email','first_name','last_name','isPublic')


admin.site.unregister(get_user_model())
admin .site.register(get_user_model(),UserProfileAdmin)

现在我看不到单独的 UserProfile ,但被集成到用户中,这就是我想要的。



我也想要搜索模型显示在用户管理员。但也是单独的。
如何注册两个(或更多)管理员用户模型?

解决方案

只需在 UserProfileAdmin Inline $ c>,然后在 UserProfileAdmin UserProfileInline SearchesInline c $ c>,然后在 admin.py 中放入 admin.site.register(Searches)。除非我误解了这个问题。


I have a search model which has a ForeignKey relation to User

class Searches(models.Model):
    user = models.ForeignKey(User)
    ......

I have a UserProfile model which has a OnetoOne Relationship to User

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    photo = models.ImageField(upload_to='profile_images', blank=True)
    ispublic=models.NullBooleanField()

I have attached UserProfile in admin.py as follows:

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False

class UserProfileAdmin(UserAdmin):

    inlines=(UserProfileInline, )
    list_filter = UserAdmin.list_filter + ('email',)
    list_display=('username','email','first_name','last_name','isPublic')


admin.site.unregister(get_user_model())
admin.site.register(get_user_model(), UserProfileAdmin)

Now I do not see a separate UserProfile but is integrated into User, which is what I want.

I also want to have Search model to show up in User admin. But also seperately. how can I register two (or more) Admins to User model?

解决方案

Try just putting another Inline inside the UserProfileAdmin, that will then place the UserProfileInline and SearchesInline in the UserProfileAdmin, then put admin.site.register(Searches) in admin.py. Unless I misunderstand the question.

这篇关于如何在Django中将多个ModelAdmin附加到UserAdmin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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