Ad

How To Save A List Of Numbers To Django Model

- 1 answer

I am pulling some data from an API and I want to store it in my Django model. The data is a baseball inning and then runs scored that inning and comes to me like this...

    "innings":[
    0:0
    1:3
    2:0
    3:0
    4:1
    5:2
    6:0
    7:0
    8:4
    ]

I access each individual value like...

    for game in games:
        first_inning = game['scoreboard']['score']['innings'][0]
        second_inning = game['scoreboard']['score']['innings'][1]
        etc...

But if I wanted to save all the data as it is and start the innings at 1 instead of 0, which type of field would I use and how would I do that? Would it be an ArrayField?

I really appreciate your help.

Ad

Answer

There are some ways depending on your problem.

  1. you can store your data in ArrayField. but ArrayField is only spcific to PostgreSQL database.(more information here)

  2. you can convert your data to JSON and store it in JSONField(more information about JSONField is here).

My suggestion is solution number 2 because you are reading serialized data from API.

I hope it could help you.

Ad
source: stackoverflow.com
Ad