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

    <small id='8QBh6'></small><noframes id='8QBh6'>

  • <tfoot id='8QBh6'></tfoot><legend id='8QBh6'><style id='8QBh6'><dir id='8QBh6'><q id='8QBh6'></q></dir></style></legend>
      <bdo id='8QBh6'></bdo><ul id='8QBh6'></ul>

        在 Django Rest Framework 中动态修改序列化器字段

        Dynamically modifying serializer fields in Django Rest Framework(在 Django Rest Framework 中动态修改序列化器字段)

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

                <tbody id='GJSN3'></tbody>

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

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

                  本文介绍了在 Django Rest Framework 中动态修改序列化器字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 django rest 框架文档中描述的高级序列化程序用法.http://django-rest-framework.org/api-guide/serializers.html#advanced-serializer-usage 动态修改序列化器字段

                  I'm trying to use the Advanced serializer usage described in the django rest framework documentation. http://django-rest-framework.org/api-guide/serializers.html#advanced-serializer-usage to dynamically modifying serializer field

                  这是我的序列化程序类:

                  Here is my serializer class:

                  class MovieSerializer(serializers.ModelSerializer):
                      moviework_work = MovieWorkSerializer(many=True) 
                  
                      def __init__(self, *args, **kwargs):
                          # Don't pass the 'fields' arg up to the superclass
                          dropfields = kwargs.pop('dropfields', None)
                  
                          # Instantiate the superclass normally
                          super(MovieSerializer, self).__init__(*args, **kwargs)
                  
                          if dropfields:
                              # Drop fields specified in the `fields` argument.
                              banished = set(dropfields)
                              for field_name in banished:
                                  self.fields.pop(field_name)
                      class Meta:
                          model = Movie
                          fields = ('field1','field2','moviework_work')
                  

                  这是我的观点

                  class MovieFromInterpreterViewSet(viewsets.ModelViewSet):
                      queryset = Movie.objects.all()
                      serializer_class = MovieSerializer(dropfields=('moviework_work',))
                  

                  我收到此错误:

                  TypeError: 'MovieSerializer' object is not callable
                  

                  推荐答案

                  请注意,您设置的 serializer_class 不是类,而是类的实例.您要么需要将 dropfields 设置为类的属性,(就像在您链接到的文档示例中为 fields 所做的那样),或者您需要查看覆盖视图集的 get_serializer 方法(docs).

                  Note that you are setting serializer_class not to a class, but to an instance of the class. You either need to set dropfields as an attribute on the class, (just like it does for fields in the documented example you link to) or you need to look at overriding the get_serializer method of the viewset (docs).

                  这篇关于在 Django Rest Framework 中动态修改序列化器字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                • <tfoot id='1QmqI'></tfoot>

                      <small id='1QmqI'></small><noframes id='1QmqI'>

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