简体   繁体   English

Django Model 外键到文本

[英]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.有一个问题,有一本书model,其中书名是书名,作者是作者model的ForeignKey,其中有一个名称字段。 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?我需要得到一个 Python 列表,为此我做books = list(Book.objects.values('title', 'author') ,但结果是一个列表[{'title': 'Harry Potter', 'author': 1}] . 如何确定作者是不是单位?

The quickest fix for you is books = list(Book.objects.values('title', 'author__name') . In the assumption that author model has name field.最快的解决方法是books = list(Book.objects.values('title', 'author__name') 。假设作者 model 具有名称字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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