自定义 django 评论管理槽
因此,我在我的博客应用程序中使用 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 下),但现在链接错误......模型指向:
输出一个错误:
没有这样的表:myapp_myappcomment
而不是:
这是因为管理员根据应用程序名称和模型名称形成其网址...我怎样才能更改管理员中评论模型的位置,但保留网址不变?
一定有什么办法可以做到吗?
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
---Modle2Comments
---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:
which outputs an error:
no such table: myapp_myappcomment
instead of:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还没有“覆盖”
Comment
类 - 通过子类化它,您实际上已经使用 多表继承。这就是为什么需要创建另一个表。您可以创建一个继承自
Comment
类,那么不需要创建额外的表。如果
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.You shouldn't need to set
app_label
ifMyAppComment
is defined in themyapp
app - it will be set automatically.