Ejemplo n.º 1
0
class EntrySerializer(serializers.ModelSerializer):
    def __init__(self, *args, **kwargs):
        super(EntrySerializer, self).__init__(*args, **kwargs)
        # to make testing more concise we'll only output the
        # `featured` field when it's requested via `include`
        request = kwargs.get('context', {}).get('request')
        if request and 'featured' not in request.query_params.get(
                'include', []):
            self.fields.pop('featured', None)

    included_serializers = {
        'authors': 'example.serializers.AuthorSerializer',
        'comments': 'example.serializers.CommentSerializer',
        'featured': 'example.serializers.EntrySerializer',
        'suggested': 'example.serializers.EntrySerializer',
        'tags': 'example.serializers.TaggedItemSerializer',
    }

    body_format = serializers.SerializerMethodField()
    # single related from model
    blog_hyperlinked = relations.HyperlinkedRelatedField(
        related_link_view_name='entry-blog',
        related_link_url_kwarg='entry_pk',
        self_link_view_name='entry-relationships',
        read_only=True,
        source='blog')
    # many related from model
    comments = relations.ResourceRelatedField(many=True, read_only=True)
    # many related hyperlinked from model
    comments_hyperlinked = relations.HyperlinkedRelatedField(
        related_link_view_name='entry-comments',
        related_link_url_kwarg='entry_pk',
        self_link_view_name='entry-relationships',
        many=True,
        read_only=True,
        source='comments')
    # many related from serializer
    suggested = relations.SerializerMethodResourceRelatedField(
        related_link_view_name='entry-suggested',
        related_link_url_kwarg='entry_pk',
        self_link_view_name='entry-relationships',
        source='get_suggested',
        model=Entry,
        many=True,
        read_only=True)
    # many related hyperlinked from serializer
    suggested_hyperlinked = relations.SerializerMethodHyperlinkedRelatedField(
        related_link_view_name='entry-suggested',
        related_link_url_kwarg='entry_pk',
        self_link_view_name='entry-relationships',
        source='get_suggested',
        model=Entry,
        many=True,
        read_only=True)
    # single related from serializer
    featured = relations.SerializerMethodResourceRelatedField(
        source='get_featured', model=Entry, read_only=True)
    # single related hyperlinked from serializer
    featured_hyperlinked = relations.SerializerMethodHyperlinkedRelatedField(
        related_link_view_name='entry-featured',
        related_link_url_kwarg='entry_pk',
        self_link_view_name='entry-relationships',
        source='get_featured',
        model=Entry,
        read_only=True)
    tags = TaggedItemSerializer(many=True, read_only=True)

    def get_suggested(self, obj):
        return Entry.objects.exclude(pk=obj.pk)

    def get_featured(self, obj):
        return Entry.objects.exclude(pk=obj.pk).first()

    def get_body_format(self, obj):
        return 'text'

    class Meta:
        model = Entry
        fields = ('blog', 'blog_hyperlinked', 'headline', 'body_text',
                  'pub_date', 'mod_date', 'authors', 'comments',
                  'comments_hyperlinked', 'featured', 'suggested',
                  'suggested_hyperlinked', 'tags', 'featured_hyperlinked')
        read_only_fields = ('tags', )
        meta_fields = ('body_format', )

    class JSONAPIMeta:
        included_resources = ['comments']
class EntrySerializer(serializers.ModelSerializer):
    def __init__(self, *args, **kwargs):
        super(EntrySerializer, self).__init__(*args, **kwargs)
        # to make testing more concise we'll only output the
        # `featured` field when it's requested via `include`
        request = kwargs.get("context", {}).get("request")
        if request and "featured" not in request.query_params.get("include", []):
            self.fields.pop("featured", None)

    included_serializers = {
        "authors": "example.serializers.AuthorSerializer",
        "comments": "example.serializers.CommentSerializer",
        "featured": "example.serializers.EntrySerializer",
        "suggested": "example.serializers.EntrySerializer",
        "tags": "example.serializers.TaggedItemSerializer",
    }

    body_format = serializers.SerializerMethodField()
    # single related from model
    blog_hyperlinked = relations.HyperlinkedRelatedField(
        related_link_view_name="entry-blog",
        related_link_url_kwarg="entry_pk",
        self_link_view_name="entry-relationships",
        read_only=True,
        source="blog",
    )
    # many related from model
    comments = relations.ResourceRelatedField(many=True, read_only=True)
    # many related hyperlinked from model
    comments_hyperlinked = relations.HyperlinkedRelatedField(
        related_link_view_name="entry-comments",
        related_link_url_kwarg="entry_pk",
        self_link_view_name="entry-relationships",
        many=True,
        read_only=True,
        source="comments",
    )
    # many related from serializer
    suggested = relations.SerializerMethodResourceRelatedField(
        related_link_view_name="entry-suggested",
        related_link_url_kwarg="entry_pk",
        self_link_view_name="entry-relationships",
        model=Entry,
        many=True,
    )
    # many related hyperlinked from serializer
    suggested_hyperlinked = relations.SerializerMethodHyperlinkedRelatedField(
        related_link_view_name="entry-suggested",
        related_link_url_kwarg="entry_pk",
        self_link_view_name="entry-relationships",
        model=Entry,
        many=True,
    )
    # single related from serializer
    featured = relations.SerializerMethodResourceRelatedField(model=Entry)
    # single related hyperlinked from serializer
    featured_hyperlinked = relations.SerializerMethodHyperlinkedRelatedField(
        related_link_view_name="entry-featured",
        related_link_url_kwarg="entry_pk",
        self_link_view_name="entry-relationships",
        model=Entry,
        read_only=True,
    )
    tags = relations.ResourceRelatedField(many=True, read_only=True)

    def get_suggested(self, obj):
        return Entry.objects.exclude(pk=obj.pk)

    def get_featured(self, obj):
        return Entry.objects.exclude(pk=obj.pk).first()

    def get_body_format(self, obj):
        return "text"

    class Meta:
        model = Entry
        fields = (
            "blog",
            "blog_hyperlinked",
            "headline",
            "body_text",
            "pub_date",
            "mod_date",
            "authors",
            "comments",
            "comments_hyperlinked",
            "featured",
            "suggested",
            "suggested_hyperlinked",
            "tags",
            "featured_hyperlinked",
        )
        read_only_fields = ("tags",)
        meta_fields = ("body_format",)

    class JSONAPIMeta:
        included_resources = ["comments"]