Home > Software design >  How to save a list of numbers to Django model
How to save a list of numbers to Django model

Time:02-20

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.

CodePudding user response:

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.

CodePudding user response:

One option is an ArrayField, it's 0-indexed as any python list and you cannot change that.
Another option is to model your Inning as a separate model, in case you want to perform queries like "average score on the 3rd inning" etc. You will be able to adjust inning numbers however you want them.

class Inning(models.Model):
    game = models.ForeignKey('game.Game', on_delete=models.CASCADE)
    number = models.PositiveIntegerField()
    score = models.PositiveIntegerField()

    class Meta:
         unique_together = [('game', 'number')]

CodePudding user response:

You could use the JSONField. The benefit of it is that you can format it however you want to store your data according to what you are getting from the API. In your model, you can define the field like this:

class SomeModel(models.Model):
    ....
    innings_score = models.JSONField(default=dict)

Here I would advise you to use a default because of what is mentioned in the offical docs:

If you give the field a default, ensure it’s an immutable object, such as a str, or a callable object that returns a fresh mutable object each time, such as dict or a function. Providing a mutable default object like default={} or default=[] shares the one object between all model instances.

Then you can use save your data as a normal dictionary in the model:

SomeModel.objects.create(...., innings_score={0:0,
                                              1:3,
                                              2:0,
                                              3:0,
                                              4:1,
                                              5:2,
                                              6:0,
                                              7:0,
                                              8:4})

Since this is a dictionary, you can start your data from 1 by naming your key 1 instead of 0 (i.e skip the first value), and so on.

  • Related