N
N
nurzhannogerbek2017-04-09 15:22:57
Django
nurzhannogerbek, 2017-04-09 15:22:57

How to pull list of id?

Hello! I can't solve one problem. Submit an idea please! I will be glad to any idea.
In my Django project, I use the `django-reversion` application to control changes to the "Product" model. The `"Product"` model consists of a "comments" field of type ManyToMany.
There is a page with a history of changes to the Product model, but the comments field only displays a list of comment id's. Below is an example page for illustration purposes. Not entirely informative, I would like to display the rest of the information about the comments (author, data, text) by these id. I can't figure out how to get them out.
0069718bc3694bdeb2f85635ca93b9ca.PNG
models.py:

@reversion.register()
class Product(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 product_reversions(request, product_code):
    product = get_object_or_404(Product, pk=project_code)
    versions = Version.objects.get_for_object(product)
    for version in versions:
        version.comments = Comment.objects.filter(id__in=version.field_dict['comments'])
    context = {
        'product': product,
        'versions': versions,
    }
    return render(request, 'project/product_reversions.html', context)

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-04-09
@nurzhannogerbek

for version in versions:
    version.comments = Comment.objects.filter(id__in=version.field_dict['comments'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question