0

hi guys my head is spinning trying to figure this out. I cant get a response from a streamfield api using blocks.PageChooserBlock. I need to deserialize the JSON but am out of juice.

I get this error Object of type 'TimelineEvent' is not JSON serializable

This is the Page

class Timeline(Page):
    content = StreamField([
        ('title', blocks.CharBlock(classname="full title")),
        ('time_period', TimePeriodBlock())
    ])
    content_panels = Page.content_panels + [
        StreamFieldPanel('content')
    ]
    api_fields = [
        APIField("title"),
        APIField("content")
    ]

I'm able to get data from TimePeriodBlock and it looks like this

class TimePeriodBlock(blocks.StructBlock):
    def get_api_representation(self, value, context=None):
        dict_list = []
        for item in value:
            temp_dict = {
                'period_name': value.get("period_name"),
                'description': value.get("description"),
                'duration': value.get("duration"),
                'events': value.get('events')
            }
            print(value.get('events'))
            dict_list.append(temp_dict)
            return dict_list
    period_name = blocks.CharBlock()
    description = blocks.CharBlock()
    duration = blocks.CharBlock()
    events = blocks.ListBlock(EventBlock())

BUT can't get anywhere near EventBlock

class EventBlock(blocks.StructBlock):
    def get_api_representation(self, value, context=None):
        dict_list = []
        print('here')
        print(value)
        for item in value:
            temp_dict = {
                # 'event': item.get("event"),
                'short_desc': item.get("short_desc"),
            }
            dict_list.append(temp_dict)
            print(dict_list)
        return dict_list
    event = blocks.PageChooserBlock(page_type=TimelineEvent)
    short_desc = blocks.CharBlock(max_length=250)

Event looks like this [StructValue([('event', <TimelineEvent: Dance 1700 fandango and jota >), ('short_desc', 'first event')]), StructValue([('event', <TimelineEvent: Dance 1700 contredanse>), ('short_desc', '2nd event')])]

[StructValue([('event', <TimelineEvent: Dance 1937 Trudl Dubsky>), ('short_desc', '3rd')])]

1 Answers1

1

value.get(...) will just give you the native value of the child block, not the API representation. To get that, you need to call get_api_representation on the child block:

        temp_dict = {
            'period_name': value.get("period_name"),
            'description': value.get("description"),
            'duration': value.get("duration"),
            'events': self.child_blocks['events'].get_api_representation(value.get("events"))
        }
gasman
  • 23,691
  • 1
  • 38
  • 56