自定义 django 评论管理槽

发布于 2024-12-21 20:05:45 字数 1147 浏览 0 评论 0原文

因此,我在我的博客应用程序中使用 Django 内置评论应用程序 (django.contrib.comments)。我对表单显示等进行了一些更改(如文档中所述)。最后一件让我困扰的事情是评论模型在管理中属于它自己的类别,如下所示:

我的应用
---型号1
---模块2

评论
---评论

我希望它是这样的,因为评论与 MyApp 模型相关联。

我的应用
---型号1
---模块2
---评论

我试图通过将这行代码添加到 MyApps admin.py (覆盖 Comment 类)来实现此目的,

class MyAppComment(Comment):

    class Meta(Comment.Meta):
       app_label = 'myapp'

admin.site.unregister(Comment)
admin.site.register(MyAppComment, CommentsAdmin)

并且这有效(并且 Comments 模型显示在 MyApps 下),但现在链接错误......模型指向:

http://www.mysite.com/admin/myapp/myappcomment/

输出一个错误:

没有这样的表:myapp_myappcomment

而不是:

http://www.mysite.com/admin/comments/comment/

这是因为管理员根据应用程序名称和模型名称形成其网址...我怎样才能更改管理员中评论模型的位置,但保留网址不变?

一定有什么办法可以做到吗?

So I am using Djangos builtin comments app (django.contrib.comments) for my blog application. I made several changes to the forms display, etc. (as described in the documentation). One last thing that bothers me is that the Comments model is in it's own category in the admin, like this:

MyApp
---Model1
---Modle2

Comments
---Comments

I want it to be like this since the comments are tied to the MyApp models.

MyApp
---Model1
---Modle2
---Comments

I tried to achieve this by adding this line of code to MyApps admin.py (overwriting the Comment class)

class MyAppComment(Comment):

    class Meta(Comment.Meta):
       app_label = 'myapp'

admin.site.unregister(Comment)
admin.site.register(MyAppComment, CommentsAdmin)

And this works (and the Comments model shows up under MyApps) but now the links are wrong...the model points to:

http://www.mysite.com/admin/myapp/myappcomment/

which outputs an error:

no such table: myapp_myappcomment

instead of:

http://www.mysite.com/admin/comments/comment/

This is because the admin forms it's urls according to app names and model names...how could I just change the position of the Comments model in the admin but leave the urls as they are?

There must be some way to do it?

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

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

发布评论

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

评论(1

魔法唧唧 2024-12-28 20:05:45

您还没有“覆盖” Comment 类 - 通过子类化它,您实际上已经使用 多表继承。这就是为什么需要创建另一个表。

您可以创建一个继承自Comment 类,那么不需要创建额外的表。

class MyAppComment(Comment):

    class Meta(Comment.Meta):
        proxy = True

admin.site.unregister(Comment)
admin.site.register(MyAppComment, CommentsAdmin)

如果 myapp 应用中定义了 MyAppComment,则您不需要设置 app_label - 它将自动设置。

You haven't 'overwritten' the Comment class -- by subclassing it, you've actually created a child model using multi table inheritance. This is why another table needs to be created.

You could create a proxy model that inherits from the Comment class, then no additional tables need to be created.

class MyAppComment(Comment):

    class Meta(Comment.Meta):
        proxy = True

admin.site.unregister(Comment)
admin.site.register(MyAppComment, CommentsAdmin)

You shouldn't need to set app_label if MyAppComment is defined in the myapp app - it will be set automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文