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.
- Just delete it using delete(), but record gone forever
- 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.