首页 > 解决方案 > How to get the specific field of foreignkey using values function in Django?

问题描述

Suppose Foo, Entry are two Django models, and foo is a foreignkey in Entry. The Django QuerySet Api says that,If you have a field called foo that is a foreignkey, the default values() call will return a dictionary key called foo_id( https://docs.djangoproject.com/en/dev/ref/models/querysets). My question is how to get the specific field of foreignkey foo by values()? That is, if FIELDS is a field of fool,when I get a dictionary list of Entry, how to show FIELDS in the return dictionary, not the foo_id?

标签: django-models

解决方案


您可以使用双下划线来表示外键的字段:

Entry.objects.values('foo__FIELDS')

推荐阅读