-1

Frontend (React) gives me this type of json data:

{
    "question": [{
            "id": '0',
            "title": 'Click to write the question text',
            "choice": ['Click to write Choice 1', 'Click to write Choice 2', 'Click to write Choice 3'],
            "answerType": 'singleAnswer',
            "answerStyle": 'vertical',
        },
        {
            "id": '1',
            "title": 'Click to write the question text',
            "choice": ['Click to write Choice 1', 'Click to write Choice 2', 'Click to write Choice 3'],
            "answerType": 'singleAnswer',
            "answerStyle": 'horizontal',
        },
        {
            "id": '2',
            "title": 'Click to write the question text',
            "choice": ['Click to write Choice 1', 'Click to write Choice 2', 'Click to write Choice 3'],
            "answerType": 'multipleAnswer',
            "answerStyle": 'horizontal',
        },
    ]
}

I need to save it in my SQLite database in Django.

  • How do I save the choice field?
  • If I make the Question and Choice models separately, how do I save the json choice one by one in the choice table? How to get back this data in the frontend?
  • How should I design my model? What should I need to write in my API view?
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • Hello and welcome to SO. You may not realize, but the way you formulated your question is basically asking us to design and implement this feature, which is totally outside the scope of SO. Note that your first two questions (how to save and retrieve data) have answers (either full or partial) in Django's very comprehensive documentation, and that the last one (well, the last two actually - that's two questions in one) really depends on your app's requirements and your own design choices so we can't really answer them. – bruno desthuilliers Mar 12 '19 at 10:37
  • nb: removed the reactjs and javascript tags as the question is really about Django (the fact that the client is a react app is totally irrelevant here). – bruno desthuilliers Mar 12 '19 at 10:38
  • sorry for tag. And actually i already design the database and make api for this design. but the problem is when frontend gives me a data it only gives question because i separate the question and choice model. And my api is also different. I make one api for question and another api for choice and another api for response or vote. But the problem is frontend gives me that format of data. so do i need to first loads the data and seperate the data parts by parts and then save the data in specific table?. I am a junior developer. My knowledge is kinda limited. – iftekhar prolific Mar 12 '19 at 10:44
  • 1
    You should probably add the information you provided to the question. And yes, it does make sense that the django view will do some work parsing the json if you cannot separate it properly on the fronted. – Daniel Dror Mar 12 '19 at 10:57
  • 1
    @iftekharprolific please edit your question to add those details. wrt/ the difference between what the react client send you and how your models are designed, this is a very common case (and not only for rest APIs) - your models represent a relational database schema, which almost never how the "users" (in the most general definition - here the react app is a "user" of your API) view those data, so you can't expect a one-to-one mapping. It's your job as a developer to translate between the business view and the database implementation. – bruno desthuilliers Mar 12 '19 at 11:32

1 Answers1

0

This is a very broad question, and somewhat depends on the rest of you application's design and usage.

There are several approaches to this question, which i'll try to address briefly:

  1. If you do save the choices separately (related normalization SO question), you will have a 1 to many model, or possibly even many to many model. you can read more about it here

  2. You could save them as a single (string) column in the Question model (denormalization), and concat the array either on the client side or, given the json you provided, in django. you can do such manipulations using custom fields

  3. Given always having same amount of choices (or at least constained, say at most 4 choices), you might also get away with adding a column per choice, again using custom fields, or even custom model save method.

Choosing an approach will affect how you return the data back to the client. Also, you should consider how are you going to look up question and choices, and whether you need to be able to lookup a single choice or not. You also consider that if you will need another model in the future, like Answer which relates the those same choices, you will have similiar problems, and probably the straight forward approach would be 1 (normalization into Questions and Choices).

Daniel Dror
  • 2,304
  • 28
  • 30
  • I already normalize the whole database. Please check out my database model. https://docs.google.com/spreadsheets/d/1Jaxen1EU7IdYiIQ_1T4EB53CdsS90lJsSZ_VkaT51aQ/edit#gid=0 – iftekhar prolific Mar 12 '19 at 10:28