<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 - 在序列化程序中获取相关模型字段

        时间:2023-08-30

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

                  上一篇:在 django-import-export 中处理foreignKeys的导入 下一篇:Django 错误.不能分配必须是实例

                  相关文章

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

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

                    1. <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>