How can do not delete post and comment whose deleted user in django?

Hello i recently study django and make board

my question was that

  1. how can do not delete post and comment whose deleted users

  2. how can display deleted user's name in post and comment like 'deleted user' in board's user name (not blank or Null)

yeah i know in this code user was conneted with post and comment through foreign key and on_delete=models.CASCADE and if user deleted the post and comment deleted automatically

then how can i connect this table? using nickname? it was unique but can changed

i wanna know how to solve this problem, need to some tips

class Post(models.Model):
    
        author = models.ForeignKey(User, on_delete=models.CASCADE)
        title = models.CharField(max_length=200)
        content = models.TextField()
        create_date = models.DateTimeField(default= timezone.now())
        
        def __str__(self):
            return self.title      
              
class Comment(models.Model):
    
        author = models.ForeignKey(User, on_delete=models.CASCADE)
        post = models.ForeignKey(Post, on_delete=models.CASCADE)
        content = models.TextField()
        create_date = models.DateTimeField(default=timezone.now())

Using on_delete=models.DO_NOTHING should resolve the issue.

class Post(models.Model):

    author = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    title = models.CharField(max_length=200)
    content = models.TextField()
    create_date = models.DateTimeField(default= timezone.now())
    
    def __str__(self):
        return self.title      
          
class Comment(models.Model):

    author = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    post = models.ForeignKey(Post, on_delete=models.DO_NOTHING)
    content = models.TextField()
    create_date = models.DateTimeField(default=timezone.now())
Back to Top