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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top