I’m doing a small to-to list, I’m stuck in views.py, there are to-do tasks, in-progress tasks and done tasks, I want to move a task from the to-do task list to the in-progress task list, I can’t figure out how to delete the data in the to-to task and make new data same as to-do task in the in-progress task at the same time. It would be great if anyone can help, I’m totally new to Django. Thanks.
”’
models.py class Todo(models.Model): project = models.ForeignKey(Project, on_delete=models.CASCADE) name = models.CharField(max_length=20) start_date = models.DateTimeField(default=datetime.datetime.now) due_date = models.DateTimeField(default=datetime.datetime.now) def __str__(self): return self.text[:60] + "..." class Progress(models.Model): project = models.ForeignKey(Project, on_delete=models.CASCADE) name = models.ForeignKey(Todo, on_delete=models.CASCADE) start_date = models.DateTimeField(default=datetime.datetime.now) due_date = models.DateTimeField(default=datetime.datetime.now) def __str__(self): return self.text
”’
Advertisement
Answer
I would suggest only having one class, with an extra row for whether they are in progress or not. This will save you from creating and deleting django and DB objects over and over, when through their lifecycle they are the same information. You can achieve this in a few ways: a float field to track percentage of completeness, or just a boolean. For simplicity of example, I’ll use a boolean.
`class Todo(models.Model): project = models.ForeignKey(Project, on_delete=models.CASCADE) name = models.CharField(max_length=20) start_date = models.DateTimeField(default=datetime.datetime.now) due_date = models.DateTimeField(default=datetime.datetime.now) in_progress = models.BooleanField(default=False) def __str__(self): return self.name`
Then in your views you can query for those tasks that are or are not in progress. As mentioned above you could also use a float field and then do a search for anything greater that 0.