【问题标题】:Commenting app for all other apps in Django为 Django 中的所有其他应用程序评论应用程序
【发布时间】:2014-04-03 06:10:17
【问题描述】:

我有一个论坛应用。它具有 TagQuestionAnswer 三个类。

型号:

class Tag(models.Model):
    tag_name = models.CharField(max_length=100)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=True, auto_now=False)
    description = models.TextField()

    def __unicode__(self):
        return smart_unicode(self.tag_name)

class Question(models.Model):
    short_description = models.CharField(max_length=250)
    description = models.TextField()
    asked_by = models.ForeignKey(User)
    tags = models.ManyToManyField(Tag)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=True, auto_now=False)

    def __unicode__(self):
        return smart_unicode(self.short_description)

class Answer(models.Model):
    description = models.TextField()
    for_question = models.ForeignKey(Question)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=True, auto_now=False)

    def __unicode__(self):
        return smart_unicode(self.description)

我还想为 QuestionAnswers 以及我的应用程序的其他部分 提供 cmets。实现这一目标的最佳方法是什么?我的意思是,为这个用例设计 django 模型的正确方法是什么?我必须为此使用内容类型吗?

【问题讨论】:

  • Django自带cmets模型,docs.djangoproject.com/en/dev/ref/contrib/comments能满足你的需求吗?
  • 我想,它已被弃用。但我检查了网址,并不太喜欢这个comment framework has been deprecated and is no longer supported. Most users will be better served with a custom solution。你能告诉我其他方式吗?
  • 罗宾,你是怎么回答下面这个问题的?
  • @halfer 我最终使用了通用外键。
  • 请您添加一个答案,然后以某种方式承认克雷格的努力?无视他们的善意是不礼貌的。

标签: django django-models django-contenttypes


【解决方案1】:

您是否考虑过在您的models.py 中添加一个ForeignKey 类注释?

模型非常简单:

class Comment(models.Model):
     *attached_to* = models.ForeignKey(CONTENT)
     body = models.TextField()
     parent = models.ForeignKey('self', related_name="children")

other fields...

您会为每种类型的内容使用相同的 cmets,还是希望每种类型的内容都有独特的行为?

对于一个应用程序,上述模型是最简单的。

对于多个应用程序,您最好 - 我认为 - 将 Comments 分解为一个独立的应用程序,该应用程序调用 ForeignKey (Foreign key from one app into another in Django) 中的适当应用程序

如果您正在寻找一个通用的评论系统,上述模型可以工作,但您可能需要查看 GenericForeignKey (Django Model field with multiple types?)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    相关资源
    最近更新 更多