how to attach multiple ModelAdmins to UserAdmin in Django?

家住魔仙堡 提交于 2019-12-25 04:25:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/22720984/how-to-attach-multiple-modeladmins-to-useradmin-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!