gpt4 book ai didi

python - DRF 3.0.1 : . create() 方法不支持可写嵌套字段

转载 作者:太空狗 更新时间:2023-10-30 03:00:56 26 4
gpt4 key购买 nike

我刚刚更新到 Django REST Framework 3.0.1,我的测试失败了 this addition改进对嵌套创建和更新的检查(默认情况下 .create() 方法不支持可写嵌套字段。),但据我所知,我没有尝试进行任何嵌套写入。我有一个相关的 Serializer,但它被设置为 read_only=True 并且当我发布到此端点时,相关对象都已经存在。有什么想法,还是我只是做错了什么?

class Like(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField(db_index=True)
content_object = generic.GenericForeignKey()

user = models.ForeignKey(settings.AUTH_USER_MODEL)

class Meta:
unique_together = (("user", "content_type", "object_id"),)


class LikeSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True, default=serializers.CurrentUserDefault())

class Meta:
model = Like


class LikeViewSet(viewsets.ModelViewSet):
queryset = Like.objects.all()
serializer_class = LikeSerializer

def perform_create(self, serializer):
serializer.save(user=self.request.user)

def perform_update(self, serializer):
serializer.save(user=self.request.user)

打印的 LikeSerializer

LikeSerializer():
id = IntegerField(label='ID', read_only=True)
user = UserSerializer(default=CurrentUserDefault(), read_only=True):
email = EmailField(
allow_blank=True,
allow_null=True,
max_length=75,
required=False,
validators=[<UniqueValidator(queryset=User.objects.all())>])
id = IntegerField(label='ID', read_only=True)
username = CharField(
help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.',
max_length=30,
validators=[<django.core.validators.RegexValidator object>,
<UniqueValidator(queryset=User.objects.all())>])
fullname = CharField(allow_blank=True, allow_null=True, max_length=80, required=False)
original_photo = ImageField(allow_null=True, max_length=100, required=False)
about = CharField(allow_blank=True, allow_null=True, required=False, style={'type': 'textarea'})
user_following_count = ReadOnlyField()
user_followers_count = ReadOnlyField()
content_type = SerializerMethodField()
created = DateTimeField(read_only=True)
object_id = IntegerField(required=True)
content_type = PrimaryKeyRelatedField(queryset=ContentType.objects.all(), required=True)
class Meta:
validators = [<UniqueTogetherValidator(queryset=Like.objects.all(), fields=('user', 'content_type', 'object_id'))>]

我只是发布我想要点赞的对象的 object_idcontent_type。在我更新之前工作正常。

最佳答案

关于python - DRF 3.0.1 : . create() 方法不支持可写嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27452044/

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