Django-REST中关联字段上的KeyError [英] KeyError on relation field in Django-REST
问题描述
我有以下型号:
class ProductColor(models.Model):color_title = models.CharField(max_length = 50)class BasicProduct(models.Model):product_title = models.CharField(max_length = 150)product_desc = models.TextField()product_price = models.FloatField(默认= 10.0)#描述此产品可用的颜色product_colors = models.ManyToManyField(ProductColor)class BasicCartItem(models.Model):cart_product = models.ForeignKey(BasicProduct)cart_color = models.ForeignKey(ProductColor,blank = True,默认= None,null = True)
还有我的ModelSerializer:
类CartSerializer(serializers.ModelSerializer):类Meta:模型= models.BasicCartItem
我在此输入我的看法:
{'cart_product':1}
然后运行代码:
m = CartSerializer(数据=输入数据)如果m.is_valid():items = m.data#生成KeyError:u'cart_color'
堆栈跟踪的提取:
文件"python2.7/site-packages/rest_framework/serializers.py",行430,在to_representation中属性= field.get_attribute(实例)get_attribute中的第103行的文件"python2.7/site-packages/rest_framework/relations.py"返回get_attribute(instance,self.source_attrs)get_attribute中的第69行的文件"python2.7/site-packages/rest_framework/fields.py"实例=实例[attr]KeyError:u'cart_color'
任何知识渊博的人都知道这里发生了什么事?我已经在这里呆了几个小时,我不知道自己在做什么错.
谢谢.
您以错误的方式访问数据.您应该访问序列化程序上的 .validated_data
而不是 .data
.
m = CartSerializer(数据=输入数据)如果m.is_valid():items = m.validated_data#在此处访问经过验证的数据
为什么 serializer.data
方法不适用于您的情况?
在执行 .data
时,由于尚未传递 instance
参数,因此序列化程序将尝试序列化 initial_data
.它将期望所有字段都存在,但是由于数据上不存在 cart_color
,因此它将引发 KeyError
.
然后通常什么时候应该使用 serializer.data
?
序列化现有对象时,通常应使用 serializer.data
.这将要求您在创建序列化程序的实例时传递 instance
参数.
m = CartSerializer(instance = my_object)items = m.data#序列化对象时使用.data
I have the following models:
class ProductColor(models.Model):
color_title = models.CharField(max_length=50)
class BasicProduct(models.Model):
product_title = models.CharField(max_length=150)
product_desc = models.TextField()
product_price = models.FloatField(default=10.0)
# Describes what colors are available for this product
product_colors = models.ManyToManyField(ProductColor)
class BasicCartItem(models.Model):
cart_product = models.ForeignKey(BasicProduct)
cart_color = models.ForeignKey(ProductColor, blank=True, default=None, null=True)
And my ModelSerializer:
class CartSerializer(serializers.ModelSerializer):
class Meta:
model = models.BasicCartItem
I'm giving my view this input:
{'cart_product': 1}
Then running the code:
m = CartSerializer(data=inputdata)
if m.is_valid():
items = m.data # Generates KeyError: u'cart_color'
Extract of stacktrace:
File "python2.7/site-packages/rest_framework/serializers.py", line 430, in to_representation
attribute = field.get_attribute(instance)
File "python2.7/site-packages/rest_framework/relations.py", line 103, in get_attribute
return get_attribute(instance, self.source_attrs)
File "python2.7/site-packages/rest_framework/fields.py", line 69, in get_attribute
instance = instance[attr]
KeyError: u'cart_color'
Anyone with good knowledge knows what's going on here? I've been stuck here for hours and I don't know what I'm doing wrong..
Thanks.
You are accessing the data in a wrong way. You should access .validated_data
on the serializer instead of .data
.
m = CartSerializer(data=inputdata)
if m.is_valid():
items = m.validated_data # access validated data here
Why serializer.data
approach did not work for your case?
When you are doing .data
, the serializer will try to serialize the initial_data
as you have not passed the instance
argument. It will expect all the fields to be present but since cart_color
is not present on the data, it will raise a KeyError
.
When should you generally use serializer.data
then?
You should generally use serializer.data
when you are serializing an existing object. This would require you to pass an instance
argument when creating an instance of the serializer.
m = CartSerializer(instance=my_object)
items = m.data # use .data when serializing an object
这篇关于Django-REST中关联字段上的KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!