1
我想在管理中的ManyToMany关系上使用raw_id_fields,并且我希望每个相关对象都显示在其自己的行上(而不是单个字段中的逗号分隔列表,是默认行为)。继在野外发现的例子,好像我应该能够做到这一点:raw_id_fields和Django中的ManyToMany管理
# 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'>
我并不清楚我在做什么错在这里。感谢您的建议。
感谢阿里,但是这给了我: 'FollowersInline.raw_id_fields' 是指字段“如下'从模型'Profile_follows'中缺少。 下面存在配置文件中的字段。 – shacker 2011-05-25 07:45:42
嗯,可能我并不完全理解你想做的事情:你正在试图将“追随者”和“简介”联系起来,对吧?根据我的理解,你应该可以在'InlineAdmin'上省略'raw_id_fields =('follow',)'。然后,您可以使用内联表单将'Followers'('User')关联到'Profiles'。 – arie 2011-05-25 07:53:48
追随者不是一个单独的模型 - 'follow'是配置文件模型上的m2m字段的名称。我可以从ProfileAdmin中忽略以下内容,但是当我向ProfileAdmin添加inlines =(FollowersInline,)时,根据是使用'model = Profile'还是'model = Profile.follows.through',我得到了上述两个错误之一。 。谢谢。 – shacker 2011-05-25 16:03:04