I have a BlogPost Model, and I want to define a copy function in it to copy all the posts and also all its comments. Add to that; I need to update The time of the data_created as well. Finally, It needs to return the new ID post.
P.s: It would be great if show me how I can be tested in Django Console. How can I copy and How can I see the result.
Thank you.
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return '{}'.format(self.name)
class BlogPost(models.Model):
title = models.CharField(max_length=250)
body = models.TextField()
author = models.ForeignKey(to=Author, on_delete=models.CASCADE)
data_created = models.DateTimeField(auto_now_add=True)
************
**Here I need your help.**
def copy(self):
for comment in self.comment_set.all():
comment.pk = None
comment.blog_post.data_created = comment.blog_post.data_created.now()
comment.save()
class Comment(models.Model):
blog_post = models.ForeignKey(to=BlogPost, on_delete=models.CASCADE)
text = models.TextField()
def __str__(self):
return '{}'.format(self.text)