我有模型和序列化器,该模型中有ArrayField(postgres)。
现在,我想创建一个序列化器字段,该字段将接收列表[1,2]
并将其保存到对象,但是要在序列化器中获取列表和详细信息以显示JSON对象列表。
型号:
class User(models.Model):
email = models.EmailField('Email', unique=True, blank=False)
full_name = models.CharField(
'Full name', max_length=150, blank=True, null=True)
roles = ArrayField(
models.PositiveSmallIntegerField(),
default=list,
blank=True
)
序列化器:
class ArraySerializerField(ListField):
def __init__(self, queryset, serializer_class):
super(ArraySerializerField, self).__init__()
self.queryset = queryset
self.serializer_class = serializer_class
def to_representation(self, value):
if value:
qs = self.queryset.filter(pk__in=value)
return self.serializer_class(qs, many=True).data
return []
def to_internal_value(self, value):
super(ArraySerializerField, self).to_internal_value(value)
print(value) # [1, 2]
return value
class UserSerializer(SerializerExtensionsMixin, serializers.ModelSerializer):
roles = ArraySerializerField(queryset=Role.objects.all(), serializer_class=RoleSerializer)
class Meta:
model = User
fields = ('id', 'email', 'full_name', 'roles')
def create(self, validated_data):
print(validated_data)
# {'email': 'test@test.com', 'full_name': 'Test', 'roles': []}
user = super(UserSerializer, self).create(validated_data)
return user
现在,当我进行列表或详细请求时,一切正常,我得到了一个角色列表,如JSON。
但是当我尝试发布数据并发送此数据时:
{
"email": "test@test.com",
"full_name": "Test",
"roles": [1, 2]
}
validated_data
在create
方法中始终将角色显示为[]
,并且对象被保存而没有角色,但是从to_internal_value
打印则显示[1, 2]
。
我在做什么错?它应该保存发送的数据,因为to_internal_value
可以正常工作。
编辑:
GET和LIST响应给我正确的格式:
{
"id": 1,
"email": "test@test.com",
"full_name": "Test",
"roles": [
{
"id": 1,
"name": "Role 1"
},
{
"id": 2,
"name": "Role 2"
}
]
}
答案 0 :(得分:2)
您尝试过吗?
class UserSerializer(SerializerExtensionsMixin, serializers.ModelSerializer):
roles = serializers.ListField(child=serializers.IntegerField(), allow_empty=True, required=False)
class Meta:
model = User
fields = ('id', 'email', 'full_name', 'roles')
def create(self, validated_data):
# check validated_data here
...
我不确定 SerializerExtensionsMixin
类的性质。另外,我不确定您的自定义 queryset
serializer_class
和ListField
参数背后的意图
In [7]: from rest_framework import serializers
In [8]: class UserSerializer(serializers.Serializer): # Created a simple serializer without model
...: roles = serializers.ListField(child=serializers.IntegerField(), allow_empty=True, required=False)
...: email = serializers.EmailField()
...: full_name = serializers.CharField()
...:
In [9]: data = { # your data
...: "email": "test@test.com",
...: "full_name": "Test",
...: "roles": [1, 2]
...: }
In [10]: u = UserSerializer(data=data)
In [11]: u.is_valid()
Out[11]: True
In [12]: u.data # got valid data
Out[12]: {'roles': [1, 2], 'email': 'test@test.com', 'full_name': 'Test'}
In [13]: data["roles"] = [] # change data to accept empty list
In [14]: u = UserSerializer(data=data)
In [15]: u.is_valid()
Out[15]: True
In [16]: u.data # got validated data with empty list
Out[16]: {'roles': [], 'email': 'test@test.com', 'full_name': 'Test'}
In [17]: data["roles"] = ["foo","bar"] #added string to the list
In [18]: u = UserSerializer(data=data)
In [19]: u.is_valid() # validation failed
Out[19]: False
In [20]: u.errors
Out[20]: {'roles': {0: [ErrorDetail(string='A valid integer is required.', code='invalid')], 1: [ErrorDetail(string='A valid integer is required.', code='invalid')]}}
创建一个 RoleSerializer
并在 UserSerializer
class RoleSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = ('id', 'name')
class UserSerializer(SerializerExtensionsMixin, serializers.ModelSerializer):
roles = serializers.ListField(child=serializers.IntegerField(), allow_empty=True, required=False)
class Meta:
model = User
fields = ('id', 'email', 'full_name', 'roles')
def create(self, validated_data):
# check validated_data here
...
def to_representation(self, instance):
rep = super().to_representation(instance)
rep['roles'] = RoleSerializer(Role.objects.filter(id__in=rep['roles']), many=True).data
return rep
使用自定义数组字段
class ArrayField(serializers.ListField):
def __init__(self, *args, **kwargs):
self.queryset = kwargs.pop('queryset', None)
self.serializer_class = kwargs.pop('serializer_class', None)
super().__init__(*args, **kwargs)
def to_representation(self, data):
qs = self.queryset.filter(id__in=data)
serializer = self.serializer_class(qs,many=True)
return serializer.data
class UserSerializer(SerializerExtensionsMixin, serializers.ModelSerializer):
roles = ArrayField(queryset=Role.objects.all(), serializer_class=RoleSerializer)
class Meta:
model = User
fields = ('id', 'email', 'full_name', 'roles')
def create(self, validated_data):
# check validated_data here
...
答案 1 :(得分:0)
尝试切换到PrimaryKeyRelatedField
。虽然您需要更改用户模型以使用实际关系。通常这是一个好主意,因为它将帮助您的项目加强数据完整性。
class User(models.Model):
email = models.EmailField('Email', unique=True, blank=False)
full_name = models.CharField(
'Full name', max_length=150, blank=True, null=True)
roles = models.ManyToManyField(Role, blank=True)
class UserSerializer(SerializerExtensionsMixin, serializers.ModelSerializer):
roles = serializers.PrimaryKeyRelatedField(
many=True,
queryset=Role.objects.all(),
)
class Meta:
model = User
fields = ('id', 'email', 'full_name', 'roles')
def create(self, validated_data):
print(validated_data)
# {'email': 'test@test.com', 'full_name': 'Test', 'roles': []}
user = super(UserSerializer, self).create(validated_data)
return user