N
N
nurzhannogerbek2017-04-07 22:06:00
Django
nurzhannogerbek, 2017-04-07 22:06:00

Django-reversion pulling data from ManyToMany field?

Who faced a similar task? I am using the django-reversion application in my project .
django-reversion: https://github.com/etianen/django-reversion
There is a "Task" data model with a "comments " field (ManyToMany field). The project has a page that displays a list of all versions with detailed information about all changes in the fields. The comments field displays the id of the comments. For example: [66, 67, 68]. How can I display the rest of the information about the comment (author, text, created) instead of the id?
models.py:

@reversion.register()
class Task(models.Model):
    comments = models.ManyToManyField("Comment")

class Comment(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    text = models.TextField()
    created = models.DateTimeField(auto_now_add=True)

views.py:
def task_reversions(request, project_code, task_code):
    project = get_object_or_404(Project, pk=project_code, status='open')
    task = get_object_or_404(GroupTask, pk=group_task_code)
    versions = Version.objects.get_for_object(ask)
    context = {
        'project': project,
        'task': task,
        'versions': versions,
    }
    return render(request, 'project/task_reversions.html', context)

task_reversions.html:
{% for field_name, field_value in version.field_dict.items %}
     {{ field_name }}
     {{ field_value }}
{% endfor %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maks Nikolaev, 2017-04-11
@Maks113

Likely like this:

{% for comment in task.comments.all %}
    {% for field_name, field_value in comment.field_dict.items %}
         {{ field_name }}
         {{ field_value }}
    {% endfor %}
{% endfor %}

You can also pre-select them withselect_related()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question