Django select_related查询不会将所有值都返回给模板

时间:2017-03-23 15:29:20

标签: python django django-orm django-1.10

我有2个型号。来自django网站的人物和角色详细信息我正在鬼混。

CharactersDetails与Characters

有外键关系
class CharactersDetails(models.Model):
    character_link = models.ForeignKey(Characters)

在我看来,我这样做:

'character_info': CharactersDetails.objects.select_related('character_link').filter(character_link_id=pk)

在我的模板中,我只返回CharactersDetails表中的字段。

在manage.py shell中,我运行了相同的查询,发现它实际上从CharactersDetails和Characters(这是我想要的)返回字段,但是当视图获取上下文对象时,它只显示来自的项目CharactersDetails表。

(looking_for_guild_env)klainn:~/workspace (master) $ python manage.py shell
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
>>> from players.models import Characters,CharactersDetails
>>> myjunk = CharactersDetails.objects.select_related('character_link').filter(character_link_id=1)
>>> print(myjunk)
<QuerySet [<CharactersDetails: CharactersDetails object>]>
>>> print(myjunk.query)
SELECT "characters_details"."id", "characters_details"."character_link_id", "characters_details"."character_class_id", "characters_details"."character_race_id", "characters_details"."character_level", "characters_details"."character_armory_url", "characters_details"."character_profile_image_url", "characters_details"."character_profile_avatar_url", "characters_details"."character_profile_inset_url", "characters"."id", "characters"."character_owner_id", "characters"."character_name", "characters"."character_realm_id", "characters"."character_faction_id", "characters"."insert_date" FROM "characters_details" INNER JOIN "characters" ON ("characters_details"."character_link_id" = "characters"."id") WHERE "characters_details"."character_link_id" = 1
>>> 

我接受相同的查询并将其粘贴到manage.py dbshel​​l:

(looking_for_guild_env)klainn:~/workspace (master) $ python manage.py dbshell
sqlite> SELECT "characters_details"."id", "characters_details"."character_link_id", "characters_details"."character_class_id", "characters_details"."character_race_id", "characters_details"."character_level", "characters_details"."character_armory_url", "characters_details"."character_profile_image_url", "characters_details"."character_profile_avatar_url", "characters_details"."character_profile_inset_url", "characters"."id", "characters"."character_owner_id", "characters"."character_name", "characters"."character_realm_id", "characters"."character_faction_id", "characters"."insert_date" FROM "characters_details" INNER JOIN "characters" ON ("characters_details"."character_link_id" = "characters"."id") WHERE "characters_details"."character_link_id" = 1;
1|1|15|7|110|http://us.battle.net/wow/en/character/Stormrage/Peppiwyn/simple|stormrage/219/182881755-profilemain.jpg|stormrage/219/182881755-avatar.jpg|stormrage/219/182881755-inset.jpg|1|3|Peppiwyn|192|0|2017-03-20

我得到了所有的领域。但模板没有传递最后6个值。当我在查询集的末尾添加.values()然后执行:

时,我可以看到这一点
{{ character_info }}
我看到模板中的

<QuerySet [{'id': 1, 'character_profile_image_url': 'stormrage/219/182881755-profilemain.jpg', 'character_race_id': 7, 'character_level': 110, 'character_profile_avatar_url': 'stormrage/219/182881755-avatar.jpg', 'character_profile_inset_url': 'stormrage/219/182881755-inset.jpg', 'character_armory_url': 'http://us.battle.net/wow/en/character/Stormrage/Peppiwyn/simple', 'character_link_id': 1, 'character_class_id': 15}]>

我做错了什么或者我是否应该从模板中调用这些值?

感谢阅读。

2 个答案:

答案 0 :(得分:1)

queryset方法.values()不会隐含地添加所选外键的属性。这是一个extract of the documentation

  

值得一提的一些细微之处:

     
      
  • 如果您有一个名为foo的字段ForeignKey默认values()调用将返回一个名为foo_id的字典键,因为这是存储的隐藏模型属性的名称实际值(foo属性指的是相关模型)。当您调用values()并传入字段名称时,您可以传入foo或foo_id,然后您将返回相同的内容(字典键将与您传入的字段名称匹配)。
  •   

您需要将字段列为values('character_link__field1', 'character_link__field2'...)个参数...或者只是不使用它并处理ORM对象,这些对象将使用在一个查询中检索的数据。

使用django-debug-toolbar之类的工具,您可以检查您的请求,以确保在渲染模板时只进行一次SQL查询。

答案 1 :(得分:0)

执行select_related('character_link')没有将属性添加到character_info实例,它只是在character_info的同时从数据库中提取character_link,内部联接。

在模板中,您可以访问character_link实例{{ character_info.character_link }}

如果您使用values(),则必须明确包含character_link中的字段:

.values('field1', 'field2', 'character_link__field', ...)