gpt4 book ai didi

json - Django休息框架: Deserializing and get the primary key from validated_data

转载 作者:行者123 更新时间:2023-12-01 19:00:03 25 4
gpt4 key购买 nike

我定义了一个嵌套模型Product,如下所示。每个 Product 可以属于多个 Productlist

class Product(models.Model):
product_id = models.AutoField(primary_key=True)
product_name = models.CharField(max_length=50)

class Productlist(models.Model):
productlist_id = models.AutoField(primary_key=True)
productlist_name = models.CharField(max_length=50)
product = models.ManyToManyField(Product, related_name='productlists')

对应的序列化器是:

class ProductlistSerializer(serializers.ModelSerializer):
class Meta:
model = Productlist
fields = ('productlist_id', 'productlist_name',)

class ProductSerializer(serializers.ModelSerializer):
productlists = ProductlistSerializer(many=True, required=False)

class Meta:
model = Product
fields = ('product_id', 'product_name', 'product lists')

def create(self, validated_data):
#some codes

当我POST一个新的Product时(url(r'^api/products/$',views.ProductEnum.as_view() ),我想更新产品列表,以便将新产品添加到相应的产品列表中。我更喜欢使用的 JSON 文件是:

{
"product_name": "product1"
"productlist": [
{
"productlist_id": 1,
"productlist_name": "list1",
},
{
"productlist_id": 2,
"productlist_name": list2"
}
]
}

问题是我无法从 validated_data 获取 productlist_id。在 Django Rest Framework 中,您始终需要调用 to_internal_value() 来反序列化数据并生成 validated_data。经过一番调试,我检查了DRF的代码,并在to_internal_value()中找到了以下代码片段:

def to_internal_value(self, data):
"""
Dict of native values <- Dict of primitive datatypes.
"""
if not isinstance(data, dict):
message = self.error_messages['invalid'].format(
datatype=type(data).__name__
)
raise ValidationError({
api_settings.NON_FIELD_ERRORS_KEY: [message]
})

ret = OrderedDict()
errors = OrderedDict()
fields = [
field for field in self.fields.values()
if (not field.read_only) or (field.default is not empty)
]

for field in fields:
validate_method = getattr(self, 'validate_' + field.field_name, None)
primitive_value = field.get_value(data)
try:
validated_value = field.run_validation(primitive_value)
if validate_method is not None:
validated_value = validate_method(validated_value)
except ValidationError as exc:
errors[field.field_name] = exc.detail
except DjangoValidationError as exc:
errors[field.field_name] = list(exc.messages)
except SkipField:
pass
else:
set_value(ret, field.source_attrs, validated_value)

if errors:
raise ValidationError(errors)

return ret

请注意,to_internal_value 的字段已忽略 IntegerField(read_only=True),因为它无法满足以下条件:

        fields = [
field for field in self.fields.values()
if (not field.read_only) or (field.default is not empty)
]

因此 validated_data 将仅包含以下数据:

{
"product_name": "product1"
"productlist": [
{
"productlist_name": "list1",
},
{
"productlist_name": list2"
}
]
}

如何获取产品列表的主键?提前致谢!

最佳答案

经过一番挖掘,我发现 read_only 字段仅用于输出呈现。你可以在官方github上找到类似的问题link Django REST 框架

因此解决方案是覆盖序列化器中的 read_only 字段,如下所示:

class ProductlistSerializer(serializers.ModelSerializer):
productlist_id = serializers.IntegerField(read_only=False)

class Meta:
model = Productlist
fields = ('productlist_id', 'productlist_name',)

关于json - Django休息框架: Deserializing and get the primary key from validated_data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28247839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com