问题描述
我正在使用 django-mptt
为我的 Section
模型创建一个树状结构。不幸的是,当我使用 drf-writable-nested
对其进行序列化时,出现错误。此错误仅在将 url
字段添加到序列化程序时发生。
此外,当我从自定义查找字段中删除 uuid
时,错误仅替换为 pk
。
我发现了这个:https://stackoverflow.com/a/55173445/9137820,但我没有直接访问任何数据,所以我不确定这可能是什么问题。
代码:
# models.py
class Section(MPTTModel,TimeStampedModel):
uuid = models.UUIDField(default=uuid_lib.uuid4,editable=False)
name = models.CharField(max_length=255,unique=True)
objects = TreeManager()
parent = TreeForeignKey('self',related_name='section_children',on_delete=models.CASCADE,null=True,blank=True)
# serializers.py
class SectionSerializer(UniqueFieldsMixin,WritablenestedModelSerializer):
children = serializers.ListField(source='get_children',child=RecursiveField())
class Meta:
model = Section
fields = ['url','uuid','name','children']
extra_kwargs = {
'url': {'lookup_field': 'uuid'},}
# views.py
class SectionDetail(generics.RetrieveUpdateDestroyAPIView,viewsets.GenericViewSet):
queryset = Section.objects.all()
serializer_class = SectionSerializer
lookup_field = 'uuid'
追溯:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py",line 47,in inner
response = get_response(request)
File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py",line 181,in _get_response
response = wrapped_callback(request,*callback_args,**callback_kwargs)
File "/usr/local/lib/python3.9/site-packages/django/views/decorators/csrf.py",line 54,in wrapped_view
return view_func(*args,**kwargs)
File "/usr/local/lib/python3.9/site-packages/rest_framework/viewsets.py",line 125,in view
return self.dispatch(request,*args,**kwargs)
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py",line 509,in dispatch
response = self.handle_exception(exc)
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py",line 469,in handle_exception
self.raise_uncaught_exception(exc)
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py",line 480,in raise_uncaught_exception
raise exc
File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py",line 506,in dispatch
response = handler(request,**kwargs)
File "/usr/local/lib/python3.9/site-packages/rest_framework/mixins.py",line 75,in update
return Response(serializer.data)
File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py",line 548,in data
ret = super().data
File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py",line 246,in data
self._data = self.to_representation(self.instance)
File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py",line 515,in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "/usr/local/lib/python3.9/site-packages/rest_framework/fields.py",line 1661,in to_representation
return [self.child.to_representation(item) if item is not None else None for item in data]
File "/usr/local/lib/python3.9/site-packages/rest_framework/fields.py",in <listcomp>
return [self.child.to_representation(item) if item is not None else None for item in data]
File "/usr/local/lib/python3.9/site-packages/rest_framework/serializers.py",in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "/usr/local/lib/python3.9/site-packages/rest_framework/relations.py",line 399,in to_representation
url = self.get_url(value,self.view_name,request,format)
File "/usr/local/lib/python3.9/site-packages/rest_framework/relations.py",line 335,in get_url
lookup_value = getattr(obj,self.lookup_field)
Exception Type: AttributeError at /api/v1/sections/3b15cbda-f61e-4a00-89fb-817beed10b14/
Exception Value: 'collections.OrderedDict' object has no attribute 'uuid'
解决方法
我做了一些挖掘,这就是我能找到的。 Dictionaries 和 OrderedDicts 不会将键和值保存为类属性。因此您无法使用 getattr()
检索它们。您需要从 dict.keys()
请求方法或属性,例如 dict.values()
或 OrderedDict
。您不能使用密钥名称。
看这个例子:
from collections import OrderedDict
g = OrderedDict()
g['test'] = 1
g = getattr(g,'values')
print(list(g())[0])
这很好用,但如果你改变了
g = getattr(g,'values')
到
g = getattr(g,'test')
它产生与您遇到的相同的错误