Django管理中的raw_id_fields和ManyToMany

问题描述

|| 我想在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,)
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...