What is better practices to delete data ?

Hello,
I’m new to Django and still learning new things. My question is what are better practices for deleting data from a database?
For example, I created a simple model ToDoList:
models.py

class ToDoList(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="todolist", null=True)
    name = models.CharField(max_length=200)

    def __str__(self) -> str:
        return self.name

User can create a ToDoList and delete it. In my head, there are 2 possible ways to delete it.

  1. Just delete it using delete(), but record gone forever
  2. Modify ToDoList model:
class ToDoList(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="todolist", null=True)
    name = models.CharField(max_length=200)
   deleted = models.BooleanField(default=False)

    def __str__(self) -> str:
        return self.name

In this option, I just changed deleted to True.

So what is better in the real world? I’m just curious what experience developers will say about it.

It’s not a question of “better” or “worse”. It’s an architectural decision that should be made based on business requirements.

Note: There’s a third option, too. You can delete the row from the primary table and insert it into an archive table.

1 Like

4: use django-pghistory to keep an audit trail of your record
5: use a database that supports table version history from SQL:2011(mariadb, SQL server)

2 Likes