Проблема сериализации Django/Wagtail, показывающая <modelcluster.fields.create_deferring_foreign_related_manager>.

У меня есть следующие модели:

from django.db import models
from modelcluster.fields import ParentalKey
from modelcluster.models import ClusterableModel
from wagtail.admin.edit_handlers import FieldPanel, InlinePanel
from wagtail.core.models import Page


class BaseCta(models.Model):

    text = models.TextField()
    destination_url = models.URLField()

    panels = [
        FieldPanel("text"),
        FieldPanel("destination_url"),
    ]

    def __str__(self):
        return str(
            dict(
                title=self.text,
                destination_url=self.destination_url
            )
        )


class Reports(Page):

    content_panels = Page.content_panels + [
        InlinePanel("nav", label="Reports CTAs (Calls to Action)"),
        InlinePanel("hero", label="Hero entry"),
    ]

    def __str__(self):
        return str(
            dict(
                title=self.title,
                nav=self.nav,
                hero=self.hero
            )
        )


class ReportsCta(BaseCta):
    report = ParentalKey(Reports, related_name="nav", on_delete=models.CASCADE)


class ReportsHero(ClusterableModel):
    report = ParentalKey(Reports, related_name='hero', on_delete=models.CASCADE)
    image_url = models.URLField(blank=False, null=False)
    title = models.CharField(max_length=200, blank=False, null=False)
    subtitle = models.CharField(max_length=200)
    description = models.TextField()

    panels = [
        FieldPanel("image_url"),
        FieldPanel("title"),
        FieldPanel("subtitle"),
        FieldPanel("description"),
        InlinePanel("cta", label=" Hero CTA (Calls to Action)")
    ]

    def __str__(self):
        return str(
            dict(
                image_url=self.image_url,
                title=self.title,
                subtitle=self.subtitle,
                description=self.description,
                cta=self.cta
            )
        )


class HeroCta(BaseCta):
    reports_hero = ParentalKey(
        ReportsHero,
        related_name="cta",
        on_delete=models.CASCADE,
        unique=True
    )

и следующие сериализаторы:

from rest_framework import serializers

from .models import (
    Reports,
    ReportsHero,
    ReportsCta,
    HeroCta
)


class ReportsCtaSerializer(serializers.ModelSerializer):
    class Meta:
        model = ReportsCta
        fields = (
            "text",
            "destination_url",
        )


class HeroCtaSerializer(serializers.ModelSerializer):

    class Meta:
        model = HeroCta
        fields = (
            "text",
            "destination_url",
        )


class ReportsHeroSerializer(serializers.ModelSerializer):
    cta = serializers.StringRelatedField(many=True, read_only=True)

    class Meta:
        model = ReportsHero
        fields = (
            "image_url",
            "title",
            "subtitle",
            "description",
            "cta",
        )


class ReportsSerializer(serializers.ModelSerializer):
    nav = serializers.StringRelatedField(many=True)
    hero = serializers.StringRelatedField(many=True)

    class Meta:
        model = Reports
        fields = ("title", "nav", "hero")

Когда я выполняю rest call, CTA, вложенный в поле hero, не сериализуется должным образом:

[
    {
        "title": "reports",
        "nav": [
            "{'title': 'some text', 'destination_url': 'http://www.google.com'}"
        ],
        "hero": [
            "{
                'image_url': 'http://www.google.com', 
                'title': 'title 123', 
                'subtitle': 'subtiltle 345', 
                'description': 'description', 
                'cta': <modelcluster.fields.create_deferring_foreign_related_manager.<locals>.DeferringRelatedManager object at 0x7f4a16519e20>}"
        ]
    }
]

Как я могу сделать так:

[
    {
        "title": "reports",
        "nav": [
            "{'title': 'some text', 'destination_url': 'http://www.google.com'}"
        ],
        "hero": [
            "{
                'image_url': 'http://www.google.com', 
                'title': 'title 123', 
                'subtitle': 'subtiltle 345', 
                'description': 'description', 
                'cta': {
                    'title': 'some text', 
                    'destination_url': 'http://www.google.com'
                 }
        ]
    }
]

Вернуться на верх