升级到 Django 2 后的 TypeError 和 AssertionError

TypeError and AssertionError after upgrade to Django 2

我的任务是将 Django REST 框架项目从 Django 1.8 升级到 Django 2.x。我已经将整个代码从 python 2.7 移植到 python 3.7,从 Django 1.8 移植到 2.0.13。我正在使用虚拟环境。

我在 python 3.7 和 Django 2.0.13 以及 RESTframework 3.11 上得到了 运行,尽管我 运行 在尝试创建新对象时遇到了问题。

这是我的问题的追溯:

Traceback (most recent call last):
  File "C:\Users\user1\Envs\projpy3\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\user1\Envs\projpy3\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\user1\Envs\projpy3\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\user1\Envs\projpy3\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\user1\projects\proj_py3\rest_framework\viewsets.py", line 114, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\user1\projects\proj_py3\rest_framework\views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\user1\projects\proj_py3\rest_framework\views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\user1\projects\proj_py3\rest_framework\views.py", line 476, in raise_uncaught_exception
    raise exc
  File "C:\Users\user1\projects\proj_py3\rest_framework\views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\user1\projects\proj_py3\rest_framework\mixins.py", line 18, in create
    serializer.is_valid(raise_exception=True)
  File "C:\Users\user1\projects\proj_py3\rest_framework\serializers.py", line 219, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "C:\Users\user1\projects\proj_py3\rest_framework\serializers.py", line 418, in run_validation
    value = self.to_internal_value(data)
  File "C:\Users\user1\projects\proj_py3\rest_framework\serializers.py", line 471, in to_internal_value
    for field in fields:
  File "C:\Users\user1\projects\proj_py3\rest_framework\serializers.py", line 354, in _writable_fields
    for field in self.fields.values():
  File "C:\Users\user1\Envs\projpy3\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\user1\projects\proj_py3\rest_framework\serializers.py", line 348, in fields
    for key, value in self.get_fields().items():
  File "C:\Users\user1\projects\proj_py3\rest_framework\serializers.py", line 1027, in get_fields
    field_names = self.get_field_names(declared_fields, info)
  File "C:\Users\user1\projects\proj_py3\rest_framework\serializers.py", line 1128, in get_field_names
    serializer_class=self.__class__.__name__
AssertionError: The field 'participantIDs' was declared on serializer OrderSerializer, but has not been included in the 'fields' option.

我不明白为什么这个异常只出现在新版本的代码中,因为我没有更改 modelsserializersviews,除了来自版本更新。

这就是有问题的 serializer 的样子:

class OrderSerializer(serializers.ModelSerializer):
    created_by = ShortPersonSerializer(read_only=True, required=False)
    modified_by = ShortPersonSerializer(read_only=True, required=False)
    customer = OrderPersonSerializer(required=False, allow_null=True, partial=True)
    ...
    ...
    participantIDs = serializers.SlugRelatedField(many=True, required=False, allow_null=True, slug_field='id', source='participants', queryset=Person.objects.all())
    ...
    ...
    
    class Meta:
        model = Order
        fields = ('id', 'title','customer', 
                    'customer_copy_id', 'customer_copy_salutation', 'customer_copy_first_name', 'customer_copy_last_name', 'created_at', 'created_by', 'modified_at', 'modified_by')

     def create(self, validated_data):
        customer_data = validated_data.pop('customer', None)
        participants_data = validated_data.pop('participants', None)

        if customer_data and customer_data is not None:
            validated_data['customer'] = Person(pk=customer_data['id'])

        order = Order.objects.create(**validated_data)

        if participants_data and participants_data is not None:
            setattr(order, 'participants', participants_data)      # line 1
            order.save()                                           # line 2
...           

附加信息: 在检测到这个错误之前,我得到了一个不同的错误,回溯如下:

Traceback (most recent call last):
  ...
  ...
  File "C:\Users\user1\projects\proj_py3\rest_framework\serializers.py", line 204, in save
    self.instance = self.create(validated_data)
  File "C:\Users\user1\projects\proj_py3\orders\serializers.py", line 90, in create
    setattr(order, 'participants', participants_data)
  File "C:\Users\user1\Envs\projpy3\lib\site-packages\django\db\models\fields\related_descriptors.py", line 509, in __set__
    % self._get_set_deprecation_msg_params(),
TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use participants.set() instead.

所以我把 serializer 改成了这样:

...
...
        if participants_data and participants_data is not None:
            #setattr(order, 'participants', participants_data)      # line 1
            order.save()                                           # line 2
            order.participants.set(participants_data)
...

自从我这样做后,我得到的是 AssertionError 而不是 TypeError,而且我不知道如何解决这个问题。当然我改回了代码,但我也删除了项目目录中的所有编译文件,我删除了虚拟环境并创建了一个新的,我 运行 在 Windows 上进行了磁盘清理,我甚至卸载 Python37 以便重新安装。中间总是重启。都无济于事。


更新:

我将项目 serializers.py 文件中的 updatecreate 方法与我正在使用的 rest_framework 版本中的方法进行了比较:rest_framework/serializers.py on GitHub

我根据较新版本的 Django REST 框架更改了它们(代码是为旧版本开发的)。

我还检查了项目中所有其他应用程序的所有 serializers,我发现,除了相关应用程序 orders 之外,所有应用程序都具有 attribute participantsID 在他们 SerializerMeta class 中,以防它应该通过另一个应用程序访问另一个应用程序 Serializer

在那种情况下,就像评论中建议的那样:AssertionError 一直在那里。

除了将 attribute 添加到 fields 之外别无他法。

participantIDs 添加到 fields 属性中 OrderSerializer Metaclass

class OrderSerializer(serializers.ModelSerializer):
    # other code snippets
    class Meta:
        model = Order
        fields = (other-fields, <b>'participantIDs'</b>)
    # other code snippets