<bdo id='nhuUs'></bdo><ul id='nhuUs'></ul>

  • <tfoot id='nhuUs'></tfoot>
  • <legend id='nhuUs'><style id='nhuUs'><dir id='nhuUs'><q id='nhuUs'></q></dir></style></legend>

    1. <i id='nhuUs'><tr id='nhuUs'><dt id='nhuUs'><q id='nhuUs'><span id='nhuUs'><b id='nhuUs'><form id='nhuUs'><ins id='nhuUs'></ins><ul id='nhuUs'></ul><sub id='nhuUs'></sub></form><legend id='nhuUs'></legend><bdo id='nhuUs'><pre id='nhuUs'><center id='nhuUs'></center></pre></bdo></b><th id='nhuUs'></th></span></q></dt></tr></i><div id='nhuUs'><tfoot id='nhuUs'></tfoot><dl id='nhuUs'><fieldset id='nhuUs'></fieldset></dl></div>

      <small id='nhuUs'></small><noframes id='nhuUs'>

        Django Rest Framework - 在序列化程序中获取相关模型字段

        Django Rest Framework - Get related model field in serializer(Django Rest Framework - 在序列化程序中获取相关模型字段)

            <small id='qMdvV'></small><noframes id='qMdvV'>

            <legend id='qMdvV'><style id='qMdvV'><dir id='qMdvV'><q id='qMdvV'></q></dir></style></legend>
            <i id='qMdvV'><tr id='qMdvV'><dt id='qMdvV'><q id='qMdvV'><span id='qMdvV'><b id='qMdvV'><form id='qMdvV'><ins id='qMdvV'></ins><ul id='qMdvV'></ul><sub id='qMdvV'></sub></form><legend id='qMdvV'></legend><bdo id='qMdvV'><pre id='qMdvV'><center id='qMdvV'></center></pre></bdo></b><th id='qMdvV'></th></span></q></dt></tr></i><div id='qMdvV'><tfoot id='qMdvV'></tfoot><dl id='qMdvV'><fieldset id='qMdvV'></fieldset></dl></div>

              <bdo id='qMdvV'></bdo><ul id='qMdvV'></ul>
              <tfoot id='qMdvV'></tfoot>

                    <tbody id='qMdvV'></tbody>
                • 本文介绍了Django Rest Framework - 在序列化程序中获取相关模型字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从 Django Rest 框架返回一个 HttpResponse,包括来自 2 个链接模型的数据.这些模型是:

                  I'm trying to return a HttpResponse from Django Rest Framework including data from 2 linked models. The models are:

                  class Wine(models.Model):
                  
                      color = models.CharField(max_length=100, blank=True)
                      country = models.CharField(max_length=100, blank=True)
                      region = models.CharField(max_length=100, blank=True)
                      appellation = models.CharField(max_length=100, blank=True)
                  
                  class Bottle(models.Model):
                  
                      wine = models.ForeignKey(Wine, null=False)
                      user = models.ForeignKey(User, null=False, related_name='bottles')
                  

                  我想要一个包含来自相关 Wine 的信息的 Bottle 模型的序列化程序.

                  I'd like to have a serializer for the Bottle model which includes information from the related Wine.

                  我试过了:

                  class BottleSerializer(serializers.HyperlinkedModelSerializer):
                      wine = serializers.RelatedField(source='wine')
                  
                      class Meta:
                          model = Bottle
                          fields = ('url', 'wine.color', 'wine.country', 'user', 'date_rated', 'rating', 'comment', 'get_more')
                  

                  这不起作用.

                  有什么想法可以做到吗?

                  Any ideas how I could do that?

                  谢谢:)

                  推荐答案

                  就这么简单,将 WineSerializer 添加为字段即可解决.

                  Simple as that, adding the WineSerializer as a field solved it.

                  class BottleSerializer(serializers.HyperlinkedModelSerializer):
                      wine = WineSerializer(source='wine')
                  
                      class Meta:
                          model = Bottle
                          fields = ('url', 'wine', 'user', 'date_rated', 'rating', 'comment', 'get_more')
                  

                  与:

                  class WineSerializer(serializers.HyperlinkedModelSerializer):
                  
                      class Meta:
                          model = Wine
                          fields = ('id', 'url', 'color', 'country', 'region', 'appellation')
                  

                  感谢@mariodev 的帮助 :)

                  Thanks for the help @mariodev :)

                  这篇关于Django Rest Framework - 在序列化程序中获取相关模型字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    <tbody id='k2di6'></tbody>

                  • <bdo id='k2di6'></bdo><ul id='k2di6'></ul>
                      <tfoot id='k2di6'></tfoot>

                      <small id='k2di6'></small><noframes id='k2di6'>

                        • <i id='k2di6'><tr id='k2di6'><dt id='k2di6'><q id='k2di6'><span id='k2di6'><b id='k2di6'><form id='k2di6'><ins id='k2di6'></ins><ul id='k2di6'></ul><sub id='k2di6'></sub></form><legend id='k2di6'></legend><bdo id='k2di6'><pre id='k2di6'><center id='k2di6'></center></pre></bdo></b><th id='k2di6'></th></span></q></dt></tr></i><div id='k2di6'><tfoot id='k2di6'></tfoot><dl id='k2di6'><fieldset id='k2di6'></fieldset></dl></div>
                          <legend id='k2di6'><style id='k2di6'><dir id='k2di6'><q id='k2di6'></q></dir></style></legend>