问题描述
我有一个搜索模型
,其中 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 $ c中放入另一个
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!