Django 管理中的 raw_id_fields 和 ManyToMany

发布于 2024-11-09 20:24:12 字数 732 浏览 4 评论 0原文

我想在管理中的多对多关系上使用 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'>

我不清楚我在这里做错了什么。感谢您的建议。

I want to use raw_id_fields on a ManyToMany relationship in the admin, and I want each related object to show up on its own row (as opposed to a comma-separated list in a single field, which is the default behavior). Following examples spotted in the wild, it seems like I should be able to do this:

# 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)

But that generates the error:

<class 'bucket.models.Profile'> has no ForeignKey to <class 'bucket.models.Profile'>

I'm not clear what I'm doing wrong here. Thanks for suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小瓶盖 2024-11-16 20:24:12

看来您为 InlineAdmin 设置了错误的模型
因为您定义的关注者模型是User,而不是Profile

查看文档 我想说你应该尝试:

class FollowersInline(admin.TabularInline):
    model = Profile.follows.through

并且

class ProfileAdmin(admin.ModelAdmin):
    ....
    exclude = ('follows',)
    inlines = (FollowersInline,)

Looks like you are setting the wrong model for your InlineAdmin
as the model for followers you are defining is User and not Profile.

Looking at the docs I'd say you should try:

class FollowersInline(admin.TabularInline):
    model = Profile.follows.through

and

class ProfileAdmin(admin.ModelAdmin):
    ....
    exclude = ('follows',)
    inlines = (FollowersInline,)
旧人 2024-11-16 20:24:12

在 m2m 连接的内联中,您应该使用 through 表,对于 raw_id_fields 设置,您应该使用该 through 表中的字段 - 该表中的字段可以是名称与您期望的不同。

您需要进入 sqlite3/psql 等终端来查看through表架构,并为raw_id_fields使用适当的字段。

class FollowersInline(admin.TabularInline):
    model = Profile.follows.through
    # raw_id_fields = ("follows",) <- wrong
    raw_id_fields = ("user",) # <- probably right, because your m2m relation with `User` table and django use name of that table to name field in `through` model

In inline for m2m connection you should use through table and for raw_id_fields setting you should use fields from that through table - inside this table fields can be name different as you expect.

You need goes to sqlite3/psql etc. terminal to see through table schema and use propper field for raw_id_fields.

class FollowersInline(admin.TabularInline):
    model = Profile.follows.through
    # raw_id_fields = ("follows",) <- wrong
    raw_id_fields = ("user",) # <- probably right, because your m2m relation with `User` table and django use name of that table to name field in `through` model
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文