ma_as
ma_as

Reputation: 1

why should we use parent in a Comment model?

what are self and parent? this is part of a comment app

models.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)

Upvotes: 0

Views: 37

Answers (1)

kiviak
kiviak

Reputation: 1103

because a comment can be another comment's child, self stands for class Comment here.
for example:

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

Upvotes: 1

Related Questions