Answer the question
In order to leave comments, you need to log in
How to display a list of activities for related data models?
Hello! Please help me figure it out.
There is a data model Project (Project). Each project has sections, so to speak. And to be more precise, there are data models Characterictic (Characteristic) and Task (Task) that are associated with the project. Users can leave comments on Characterictic and Task model entries.
I display a list of projects in the template and it is also necessary to display a list of the top 10 latest activities for each project. As you can see, all models have a revision_date field , which is responsible for the last update of the record. The Comment model also has a created field that shows when the comment was created.
For example :
DRAFT A
1) Comment was added to Problem A at 11:45.
2) Feature B was edited at 11:39.
3) Edited Task A at 11:00.
4) Created Task A at 10:45.
How to implement something like this?
models.py:
class Project(models.Model):
code = models.UUIDField(_('Code'), primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(_('Name'), max_length=250)
create_time = models.DateTimeField(auto_now_add=True) # Дата создания записи
revision_date = models.DateTimeField(_('Date of last update'), auto_now=True) # Дата последнего обновления записи
class Characteristic(models.Model):
code = models.UUIDField(_('Code'), primary_key=True, default=uuid.uuid4, editable=False)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
description = models.TextField(_('Description'))
comments = models.ManyToManyField("Comment")
create_time = models.DateTimeField(auto_now_add=True)
revision_date = models.DateTimeField(_('Date of last update'), auto_now=True)
class Task(models.Model):
code = models.UUIDField(_('Code'), primary_key=True, default=uuid.uuid4, editable=False)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
description = models.TextField(_('Description'))
comments = models.ManyToManyField("Comment")
create_time = models.DateTimeField(auto_now_add=True)
revision_date = models.DateTimeField(_('Date of last update'), auto_now=True)
class Comment(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
text = models.TextField()
created = models.DateTimeField(auto_now_add=True)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question