简体   繁体   English

为什么我们应该在评论 model 中使用 parent?

[英]why should we use parent in a Comment model?

what are self and parent?什么是自己和父母? this is part of a comment app这是评论应用程序的一部分

models.py模型.py

  class Comment(models.Model):
        user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, blank=True, null=True)
        email = models.EmailField(blank=True)
        parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)

because a comment can be another comment's child, self stands for class Comment here.因为一个评论可以是另一个评论的孩子, self在这里代表class Comment
for example:例如:

father=Comment(user=...)
father.save()
son=Comment(user=...)
son.parent=father
son.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我们应该在Django中为MongoDB创建Model字段? - Why should we create Model fields in Django for MongoDB? 为什么我们应该使用Exception作为超类,为什么不使用BaseException - why should we use Exception as a superclass, why not BaseException 为什么要在subprocess.Popen中使用stdout = PIPE? - why should we use stdout=PIPE in subprocess.Popen? 为什么我们应该在 def __init__(self, n) -> None: 中使用 -> - Why should we use -> in def __init__(self, n) -> None:? 为什么我们要在python正则表达式中使用re.purge()? - Why should we use re.purge() in python regular expression? Python-为什么我们应该使用模拟进行测试? - Python - Why we should use mock to do test? 为什么在反转python中的字符串时为什么要使用join()函数 - Why should we use join() function while reversing a string in python 我们可以使用Python脚本创建注释吗? - Can we use to Python script to create a comment? 当我们使用transform得到相同的output时为什么要使用fit_transform方法 - Why should we use the fit_transform method when we get the same output using transform Django 常用注释 model 在不同的应用程序中使用 - Django common comment model to use in different apps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM