sha*_*ker 2 django django-orm django-admin
我想在管理中的多对多关系上使用 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)
Run Code Online (Sandbox Code Playgroud)
但这会产生错误:
<class 'bucket.models.Profile'> has no ForeignKey to <class 'bucket.models.Profile'>
Run Code Online (Sandbox Code Playgroud)
我不清楚我在这里做错了什么。感谢您的建议。
看起来您为自己设置了错误的模型,InlineAdmin
因为您定义的追随者模型是 ,User而不是Profile。
看看文档我会说你应该尝试:
class FollowersInline(admin.TabularInline):
model = Profile.follows.through
Run Code Online (Sandbox Code Playgroud)
和
class ProfileAdmin(admin.ModelAdmin):
....
exclude = ('follows',)
inlines = (FollowersInline,)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6246 次 |
| 最近记录: |