自定义Django评论框架,使得评论不必是唯一的
我正在根据 Django 文档 自定义评论模型。
然而,在我的具体用例中,注释允许为空。然后我遇到的麻烦是 Comment 模型是使用 unique_together
设置的:
unique_together = [('user', 'comment', 'flag')]
关于如何覆盖它有什么想法吗?
(...或者我是否一开始就完全使用评论框架就走上了错误的道路?:)
I'm customizing the comments model per the Django documentation.
In my specific use case, however, comments are allowed to be blank. The trouble I get into then is that the Comment model is setup with an unique_together
:
unique_together = [('user', 'comment', 'flag')]
Any ideas on how I could go about overriding this?
(...or did I start off on the wrong track with using the Comments framework altogether? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
评论模型似乎没有唯一的约束。
models.py 代码
用于 contrib.comments。看起来 CommentFlag 模型具有唯一性约束,这不会影响您的空白评论。
你的问题一定出在其他地方。
我对评论应用程序不太熟悉,但您可以参考以下一些想法来解决您的问题。警告 我没有在评论应用程序上使用过这两种方法,所以我不确定使用这些方法是否会破坏评论框架的任何下游功能。如果您决定使用其中任何一个,请务必进行调查/测试。
话虽这么说,我可以想出两种方法来解决这个问题。
一起覆盖唯一的:
类 NonUniqueComment(注释):
元类(评论.元):
unique_together = []
使评论字段存储 Null 而不是数据库中的空字符串。Doesn't look like the Comment model has a unique constraint.
Code for models.py
for contrib.comments.It looks like the CommentFlag model has the uniqueness constraint which shouldn't effect you having blank comments.
Your problem must lie elsewhere.
I'm not very familiar with the comments app but here are some ideas you can look at to get around your problem.Warning I haven't used either of these methods on the comments app so I'm not sure if using these will break any downstream functions of the comments framework. Be sure to look into/test if you decide to use either of these.
That being said, I can think of 2 ways you can approach this.
Override the unique together:
class NonUniqueComment(Comment):
class Meta(Comment.Meta):
unique_together = []
Make the comments field store Null instead of empty string in the db.