我刚刚更新到Django REST Framework 3.0.1并且我的测试失败了this addition以改进对嵌套创建和更新(The .create() method does not support writable nestedfields by default.
)的检查,但据我所知,我'我没有尝试做任何嵌套写入。我有一个相关的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_id
和content_type
。在我更新之前工作正常。
答案 0 :(得分:4)