0

I have to have this in my model:

content: (Dictonary)    
      recipient: (Dictionary)
            type: (Facebook, Address or Email)
            name: (name or email)
            id: (id)
      textfields: (Array)
            title: (title)
            text: (text)
            size: (size)

I saw this post and this one, but I can't do it. For me is actually the first time that someone tells me that he needs exactly that structure in the model. I have never seem something like that before. But I'm also new :) so, maybe it's just that I don't know how to do it.

Maybe someone here with more experience than me can help me. I don't have any idea.

EDIT

I mean, I don't have to have it so in the model, but I need to accept POST, DELETE, PUT requests with that structure as well return GET request with that structure.

Community
  • 1
  • 1
Chuck Aguilar
  • 1,998
  • 1
  • 27
  • 50

1 Answers1

1

Dictionaries in python are done like this:

content = {}

type = {'Faceboook': '', 'Address': '', 'Email': ''}

recipient = {'type': type, 'name': '', id: ''}

textfields = {'title': '', 'text':'', 'size':''}

content = {'recipient': recipient, 'textfields': textfields}

> {'textfields': {'text': '', 'size': '', 'title': ''}, 'recipient': {: '', 'type': {'Faceboook': '', 'Email': '', 'Address': ''}, 'name': ''}}

Then you can just store this on a JSONFIELD.

Gustavo Reyes
  • 1,313
  • 1
  • 17
  • 31
  • Yes, sure, but what do you mean? To save them in normal table and when I return them I "build" the dictionary and when I get it I get the items and fill the table? Is it what do you mean? If it's, I'm not sure if I know how to build from the database and give it , and then get from the dict and save in the table. :D – Chuck Aguilar Jan 19 '17 at 22:55
  • Jsonfield will automatically save a dict as json & retrieve it to a dict from the database. – Gustavo Reyes Jan 19 '17 at 23:03
  • yep, it worked. I hadn't seen JSONField because it's here: ```from django.contrib.postgres.fields import JSONField``` – Chuck Aguilar Jan 20 '17 at 08:56
  • my favorite django field so far. – Gustavo Reyes Jan 20 '17 at 16:07
  • It's pretty well. Do you know if it's possible to restrict? I mean right now I just have "content = JSONFIELD()", do you know if I can restrict a bit the structure? And then I would like that the "id" of the recipient where a Foreign Key. Is all this possible? – Chuck Aguilar Jan 20 '17 at 16:10