问题描述
||
我想在admin中的ManyToMany关系上使用raw_id_fields,并且希望每个相关对象显示在其自己的行中(与单个字段中的逗号分隔列表相反,这是默认行为)。以下示例在野外发现,看来我应该能够做到:
# models.py
class Profile(models.Model):
...
follows = models.ManyToManyField(User,related_name=\'followees\')
# admin.py
class FollowersInline(admin.TabularInline):
model = Profile
raw_id_fields = (\'follows\',)
extra = 1
class ProfileAdmin(admin.ModelAdmin):
search_fields = (\'user__first_name\',\'user__last_name\',\'user__username\',)
inlines = (FollowersInline,)
admin.site.register(Profile,ProfileAdmin)
但这会产生错误:
<class \'bucket.models.Profile\'> has no ForeignKey to <class \'bucket.models.Profile\'>
我不清楚我在做什么错。感谢您的建议。
解决方法
看来您为
InlineAdmin
设定了错误的模型
因为您定义的追随者模型是User
,而不是Profile
。
查看我想说的文档,您应该尝试:
class FollowersInline(admin.TabularInline):
model = Profile.follows.through
和
class ProfileAdmin(admin.ModelAdmin):
....
exclude = (\'follows\',)
inlines = (FollowersInline,)