Reputation: 182
Here is my code in django,
class Comment(models.Model):
text = models.CharField(max_length=400)
writer = models.ForeignKey(Email, on_delete=models.DO_NOTHING)
replied_to = models.ManyToManyField('self', related_name='replied')
class Meta:
db_table = 'Comment'
when I add an instance of comment to replied_to, It adds to parent but replied instance keeps a pointer to related object in its replied_to field. Is there a way to remove pointer to related Comment without removing reply instace from parent?
Upvotes: 2
Views: 76
Reputation: 476557
Yes, you can use the .remove(..)
method here:
mycomment.replied_to.remove(othercomment)
This will not remove othercomment
from the database, but it will no longer be part of the replied_to
in the ManyToManyField
.
Note that by default ManyToManyField
relations to self
are symmetrical. That means that if one comment is in the replied_to
of another comment, then the other comment's replied_to
also contains the first comment. You can set the symmetrical=…
parameter [Django-doc] to False
to make it assymetrical:
class Comment(models.Model):
text = models.CharField(max_length=400)
writer = models.ForeignKey(Email, on_delete=models.DO_NOTHING)
replied_to = models.ManyToManyField('self', symmetrical=False, related_name='replied')
class Meta:
db_table = 'Comment'
Upvotes: 2