简体   繁体   中英

Django Model Foreign Key to text

There is a problem, there is a Book model and in it title is the name of the book, and author is the ForeignKey of the Author model where there is a name field. I need to get a Python list and for this I do books = list(Book.objects.values('title', 'author') , but the result is a list [{'title': 'Harry Potter', 'author': 1}] . How do I make sure that the author is instead of the unit?

The quickest fix for you is books = list(Book.objects.values('title', 'author__name') . In the assumption that author model has name field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM