how to attach multiple ModelAdmins to UserAdmin in

2019-08-29 05:55发布

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条回答
放我归山
2楼-- · 2019-08-29 06:08

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.

查看更多
登录 后发表回答