gpt4 book ai didi

python - Django Rest 嵌套序列化器的 AttributeError

转载 作者:行者123 更新时间:2023-12-01 02:23:27 25 4
gpt4 key购买 nike

我正在按照 Django Rest 文档编写嵌套序列化器,但它给了我属性错误。

这是我的模型:

class Objects(TimeStampModel):
projects = models.ForeignKey(Projects,related_name='proj_obj',on_delete=models.CASCADE)
object_name = models.CharField(max_length=50)
object_description = models.TextField()
object_main_table = models.CharField(max_length=50)
object_primary_key = models.CharField(max_length=50)
object_age_field = models.CharField(max_length=50)
date_format = models.CharField(max_length=50)

def __str__(self):
return self.object_name


class ObjectDefinition(TimeStampModel):
ATTRIBUTE = 'Attribute'
RELATION = 'Relation'
TYPE_CHOICES = (
(ATTRIBUTE, 'Attribute'),
(RELATION, 'Relation'),
)
obj = models.ForeignKey(Objects,related_name='obj_def',on_delete=models.CASCADE)
from_table = models.CharField(max_length=50)
from_table_field = models.CharField(max_length=50)
to_table = models.CharField(max_length=50)
to_table_field = models.CharField(max_length=50)
relation_type = models.CharField(max_length=50,choices=TYPE_CHOICES)
relation_sequence = models.CharField(max_length=50)
value_field = models.CharField(max_length=50, blank=True, null=True)

这是我的serializers.py 片段:

class ObjectDefinitionSerializer(serializers.ModelSerializer):

class Meta:
model = ObjectDefinition
fields = ('from_table','from_table_field','to_table','to_table_field','relation_type','value_field')

class ObjectSerializer(serializers.ModelSerializer):
definition = ObjectDefinitionSerializer(many=True)
object_description = serializers.CharField(required=False, allow_null=True, allow_blank=True
)

class Meta:
model = Objects
fields = ('projects','object_name','object_description','object_main_table','object_primary_key','object_age_field','date_format','definition')

def validate(self, data, *args, **kwargs):
date_format = data.get('date_format')
if date_format not in ['YYYYMMDD', 'DDMMYYYY']:
msg = ('Date format is incorrect')
raise serializers.ValidationError({'error_msg': msg})
return super(ObjectSerializer, self).validate(data, *args, **kwargs)

def create(self, validated_data):
definition_data = validated_data.pop('definition')
obj = Objects.objects.create(**validated_data)
for data in definition_data:
ObjectDefinition.objects.create(obj=obj, **data)
return obj

我的观点.py:

class CreateObject(CreateAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = ObjectSerializer

点击 POST 后,objects.create 对于两个模型都可以正常工作,但在返回 obj 时,它会抛出此错误:

异常值:

尝试在序列化器 ObjectSerializer 上获取字段 definition 的值时出现 AttributeError。序列化程序字段可能命名不正确,并且与 Objects 实例上的任何属性或键不匹配。原始异常文本是:“对象”对象没有属性“定义”。

我错过了什么?

最佳答案

ObjectDefinition.objrelated_nameobj_def,它与您的序列化程序不匹配。

您可以通过提供源参数来解决这个问题:

definition = ObjectDefinitionSerializer(source='obj_def', many=True)

关于python - Django Rest 嵌套序列化器的 AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47694599/

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