Home > database >  What Django model field to use for the following JSON data from draft.js (react-draft-wysiwyg)?
What Django model field to use for the following JSON data from draft.js (react-draft-wysiwyg)?

Time:06-03

I have a react-draft-wysiwyg component in my NextJS app, and I am trying to find a way to store that to my Django REST backend.

This is the stringified JSON I get directly from the draft component, but I am not sure how to organize the django model to store such data as well as be able to make PATCH and GET requests later. Would appreciate any help!

{
  blocks: [
    {
      key: '64o0i',
      text: 'Introduction edited',
      type: 'header-three',
      depth: 0,
      inlineStyleRanges: [],
      entityRanges: [],
      data: {},
    },
    {
      key: 'dcomv',
      text: 'this will be the awesome shit',
      type: 'unstyled',
      depth: 0,
      inlineStyleRanges: [],
      entityRanges: [],
      data: {},
    },
    {
      key: 'edvnc',
      text: 'Body paragraph',
      type: 'header-three',
      depth: 0,
      inlineStyleRanges: [
        { offset: 0, length: 14, style: 'color-rgb(0,0,0)' },
        { offset: 0, length: 14, style: 'bgcolor-rgb(255,255,255)' },
        { offset: 0, length: 14, style: 'fontsize-24' },
        {
          offset: 0,
          length: 14,
          style:
            'fontfamily-ui-sans-serif, system-ui, -apple-system, "system-ui", "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji',
        },
      ],
      entityRanges: [],
      data: {},
    },
    {
      key: 'd3sf7',
      text: 'this will be the awesome shit klasdfj lk;',
      type: 'unstyled',
      depth: 0,
      inlineStyleRanges: [
        { offset: 0, length: 41, style: 'color-rgb(0,0,0)' },
        { offset: 0, length: 41, style: 'bgcolor-rgb(255,255,255)' },
        { offset: 0, length: 41, style: 'fontsize-medium' },
        {
          offset: 0,
          length: 41,
          style:
            'fontfamily-ui-sans-serif, system-ui, -apple-system, "system-ui", "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji',
        },
      ],
      entityRanges: [],
      data: { 'text-align': 'left' },
    },
  ],
  entityMap: {},
};

CodePudding user response:

You can use Django JsonField https://docs.djangoproject.com/en/4.0/ref/models/fields/

def contact_default():
return {"email": "[email protected]"}

contact_info = JSONField("ContactInfo", default=contact_default)

can implement in models like:

models.JSONField(default=[])
  • Related