rest框架如何创建可选的嵌套对象?

时间:2015-08-18 04:33:18

标签: python django django-rest-framework

DRF 3.1.3,Django 1.8.3

我有几个模型就像下面的代码:

  

发表

class Post(models.Model):
    class Meta:
        ordering = ['-updated_time']

    author = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='+')
    circle = models.ForeignKey('circle')

    TYPE_CHOICES = (
        ('M', _('Members')),
        ('O', _('Org')),
    )
    type = models.CharField(_('Type'), choices=TYPE_CHOICES, default='M', max_length=1, help_text=u"帖子类型,是成员发帖,还是组织发帖?")

    created_time = models.DateTimeField(auto_now_add=True, db_index=True, editable=False)
    updated_time = models.DateTimeField(auto_now=True, db_index=True, editable=False)

    like = models.PositiveIntegerField(default=0)

    comments = GenericRelation(PostComment)
    images = GenericRelation(PostImage)
    likes = GenericRelation(PostLike)

    is_removed = models.BooleanField(_('Is Removed'), default=False, db_index=True)
  

RichPost

class RichPost(models.Model):
    title = models.CharField(max_length=100)
    summary = models.TextField(max_length=560)
    content = models.TextField(max_length=16000)

    post = models.OneToOneField(Post, related_name='rich')
  

SimplePost

class SimplePost(models.Model):
    content = models.TextField(max_length=560)
    post = models.OneToOneField(Post, related_name='simple')
  

PostSerializer

class PostSerializer(serializers.ModelSerializer):
    images = PostImageSerializer(many=True)

    simple = SimplePostSerializer(many=False)
    rich = RichPostSerializer(many=False)

    is_liked = serializers.SerializerMethodField('is_liked_by_user')

    def create(self, validated_data):
        simple = validated_data.pop('simple')
        rich = validated_data.pop('rich')
        post = Post.objects.create(**validated_data)
        return post

    def is_liked_by_user(self, obj):
        request = self.context['request']
        obj_type = ContentType.objects.get_for_model(obj)
        if PostLike.objects.filter(author=request.user, content_type__pk=obj_type.id, object_id=obj.id).exists():
            return True
        else:
            return False

    class Meta:
        model = Post
        fields = ('id', 'author', 'circle', 'type', 'created_time', 'updated_time', 'like', 'is_removed',
                  'images', 'comments_count', 'is_liked', 'simple', 'rich')
        read_only_fields = ('comments_count', 'is_liked', 'created_time', 'updated_time', 'like', 'author')

帖子属于RichPost和SimplePost之一。

当我创建帖子时,两者都是必需的。

例如,当我发送simple.content时,需要rich.title,rich.summary,rich.content。

然后,我找到了api文件:writable-nested-representations

我覆盖 .create()方法

但在尝试访问经过验证的数据或保存对象实例之前根本没有调用 is_valid()

任何人都有想法?感谢您的回答。

2 个答案:

答案 0 :(得分:1)

序列化程序中需要丰富和简单的帖子。你应该让它们成为可选的。如果您需要至少其中一个,则可以在validate()

中执行此操作
class PostSerializer(serializers.ModelSerializer):
    simple = SimplePostSerializer(required=False)
    rich = RichPostSerializer(required=False)
    ...
    def validate(self, data):
        data = super(PostSerializer, self).validate(data)
        if not any([data.get('simple'), data.get('rich')]):
            raise serializers.ValidationError('Either simple or rich is required')
        return data

答案 1 :(得分:0)

当我从DRF 3.1.3升级到DRF 3.2.2时,我不知道发生了什么,它只是起作用:(

通过 .is_valid()来到达 .create()