Django show name of reference not id

this simple database i created database diagram

img

then this my code for showing the data in view.py

    ...
    def get (request, photo_id)
    
    tag = PhotoTag.objects \
           .values('tag_id') \
           .filter(photo_id = photo_id) \
           .all()

    context  = {
            ...
            'my_tag': tag
}

and this is in my template

{% for data in my_tag %}
    {{ my_tag.tag_id }}
{% endfor %}

the result is number, what I need is showing the data of another table (tag table) like my_tag => tag_id => name (of another table)

when I do another way in view.py like

tag = PhotoTag.objects \
           .values('tag_id') \
           .filter(photo_id = photo_id) \
           .all()

for data in tag:
       tag_name = Tag.objects \
           .values('name') \
           .filter(id=data.get('tag_id')) \

context = {
     ...,
     'my_tag': tag_name,
}

then in template

{% for data in my_tag %}
     {{ my_tag.name }}
{% endfor %}

the result is what I need (showing the text name of references table) but it's only 1 data, it's not perfectly loop

So Can someone help me solve this problem, or have a magic trick for solve it?



Read more here: https://stackoverflow.com/questions/64895998/django-show-name-of-reference-not-id

Content Attribution

This content was originally published by BimaPria_4420 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: