python - How to change Representation of Many to Many related object in Django Rest Framework
问题描述
I want the complete related model on GET and use the id's on CREATE, UPDATE and DELETE. I try to use to_representation. So i want to create an array of dicts called users which should show the complete users.
But i get the error "unhashable type: 'ReturnDict'" when i add the dict in the object, it works fine if i would do it for a single user by writing to the array directly.
class CompanySerializer(serializers.ModelSerializer):
#users = UserSerializer(many=True)
created_at = serializers.DateTimeField()
updated_at = serializers.DateTimeField()
class Meta:
model = Company
fields = ['id', 'name', 'street', 'city', 'postal_code', 'state', 'company_form', 'users', 'created_at', 'updated_at']
def to_representation(self, instance):
representation = super(CompanySerializer, self).to_representation(instance)
representation['company_form'] = CompanyFormSerializer(instance.company_form).data
representation['users'] = []
for entry in instance.users.all():
user = {UserSerializer(entry).data}
representation['users'].extend(user)
return representation
解决方案
无需手动执行此操作,您可以将序列化程序添加到您的序列化程序,例如:
class CompanySerializer(serializers.ModelSerializer):
users = UserSerializer(read_only=True, many=True)
company_form = CompanyFormSerializer()
created_at = serializers.DateTimeField()
updated_at = serializers.DateTimeField()
class Meta:
model = Company
fields = ['id', 'name', 'street', 'city', 'postal_code', 'state', 'company_form', 'users', 'created_at', 'updated_at']
有关更多信息,请参阅Django REST 框架文档的处理嵌套对象部分。
你的to_representation
模型在两个部分是错误的:
- 您将结果包装
.data
在一个集合中,但正如您发现的那样,字典不能放在字典中,因为集合是可变的;和 - 你应该使用
.append(..)
而不是在.extend(..)
这里。
def to_representation(self, instance):
representation = super(CompanySerializer, self).to_representation(instance)
representation['company_form'] = CompanyFormSerializer(instance.company_form).data
representation['users'] = []
for entry in instance.users.all():
user = UserSerializer(entry).data
representation['users'].append(user)
return representation
但话虽如此,在我看来,旨在自己做这件事的糟糕的软件设计。Django 有很多工具可以通过 URI 等正确处理关系。
推荐阅读
- amazon-web-services - 如何从 terraform 创建 ansible 库存?
- c# - 我怎样才能让火箭围绕它的底部旋转,并统一到中间?
- python - 使用生成器为数据帧创建掩码函数
- ios - 如何保持 WidgetKit 上的现有条目刷新?
- json - Swift 中 Json 的“旧式 ASCII 属性列表”
- c# - 通过 API 将存储库从一个 azure devops 组织导入到另一个
- node.js - Socket.io v3.0.4 没有连接,v2.3 可以工作
- javascript - 如何使此框在单击时淡出
- c++ - 有什么方法可以从 VSPackage 手动触发 VS2019 中的测试发现通过?
- asp.net-core - ASP.NET Core 项目中的会话变量值问题。在页面第二次加载时获取值