Django REST Framework Integration

If you’ve got an API powered by Tom Christie’s excellent Django REST Framework you can serve the content of a TextPlusStuffField simultaneously in a variety of formats with the TextPlusStuffFieldSerializer.

Example

To demonstrate how it works we’ll use this simple model:

# myproject/content/models.py

from django.db import models

from textplusstuff.fields import TextPlusStuffField


class Content(models.Model):
    """Represents a piece of content."""
    content = TextPlusStuffField('Content')

    class Meta:
        verbose_name = 'Content Block'
        verbose_name_plural = 'Content Blocks'

OK, let’s write a simple ModelSerializer subclass to serialize Content instances:

# myproject/content/serializers.py

from rest_framework import serializers

from textplusstuff.serializers import TextPlusStuffFieldSerializer

from .models import Content


class ContentSerializer(serializers.ModelSerializer):
    """Serializes Content instances"""
    content = TextPlusStuffFieldSerializer()

    class Meta:
        model = Content
        fields = (
            'content',
        )

And here’s what it would look like serialized:

>>> from myproject.content.models import Content
>>> content = Person.objects.create(
...     content="""# Oh hello!\n\nHere's some _italic_ and **bold** text."""
... )
>>> content.save()
>>> from myproject.content.serializers import ContentSerializer
>>> content_serialized = ContentSerializer(content)
>>> content_serialized.data
{
    "content": {
        "raw_text": "# Oh hello!\n\nHere's some _italic_ and **bold** text.", # The 'raw' content of the field as it is stored in the database.
        "as_plaintext": "Oh hello!\n\nHere's some italic and bold text.", # The content of this field as plaintext (all markup/formatting and tokens removed)
        "as_markdown": "# Oh hello!\n\nHere's some _italic_ and **bold** text.", # The content of this field as markdown (with tokens removed)
        "as_html": "<h1>Oh hello!</h1>\n\n<p>Here's some <em>italic</em> and <strong>bold</strong> text.", # The content of this field as HTML with tokens rendered
        "as_html_no_tokens": "<h1>Oh hello!</h1>\n\n<p>Here's some <em>italic</em> and <strong>bold</strong> text.", # The content of this field as HTML with tokens removed
        "as_json": {
            "text_as_html": "<h1>Oh hello!</h1>\n\n<p>Here's some <em>italic</em> and <strong>bold</strong> text.",
            "text_as_markdown": "# Oh hello!\n\nHere's some _italic_ and **bold** text.",
            "content_nodes": []
        }
    }
}

Note

The example content used above doesn’t include any tokens which is why the 'as_html' and 'as_html_no_tokens' as well as the 'raw_text' and 'as_markdown' values are identical.