How to store variable length list of dict-type objects with FKs in Django SQL DB

32 views
Skip to first unread message

Kreychek

unread,
Apr 16, 2014, 11:34:30 PM4/16/14
to django...@googlegroups.com

Let's say I'm getting some JSON in the format of...

"fellowPlayers": [
            {
               "championId": 110,
               "teamId": 100,
               "summonerId": 34258805
            },
            {
               "championId": 9,
               "teamId": 100,
               "summonerId": 19923759
            },
{ "championId": 46, "teamId": 200, "summonerId": 2112359 },
... ]

...that can have a variable # of entries (from 0-11, inclusive), but each entry has those 3 attributes, 2 of which should map to other tables (championId and summonerId).

How would I want to define a table/model in Django to store the list of fellowPlayers (of unknown length)?

If any more information is needed please comment.

If this can't be directly done, how would you suggest handling such a situation? My initial thought was to hardcore in a # of fields (fellow_player_1, fellow_player_2, etc) based on what I expect the maximum # of entries to be, but that approach seems like it could be improved on a lot, although 11 isn't too bad in terms of hardcoding.

Kreychek

unread,
Apr 17, 2014, 1:55:30 AM4/17/14
to django...@googlegroups.com
To clarify, I have a model, let's call it MatchHistory, that stores a fixed set of statistics about a match (ex. points, kills, etc).

Example JSON for the stats:

u'stats': {   u'assists': 2,
              u'championsKilled': 6,
              u'doubleKills': 1,
              u'goldEarned': 7613,
              u'goldSpent': 7330,
              u'killingSprees': 2,
              u'largestKillingSpree': 3,
              u'largestMultiKill': 2,
              u'level': 13,
              u'magicDamageDealtPlayer': 39300,
              u'magicDamageDealtToChampions': 13151,
              u'magicDamageTaken': 7341,
              u'minionsKilled': 95,
              ...
}


These will be singly present for each match.

However, for any given match, in addition to the above data, there is another JSON object that I receive, listing the # of "fellowPlayers" (teammates and/or opponents) which can vary from 0-11 inclusive. For every fellowPlayer, their championId and summonerId would be linked to the model. These are the character they controlled in that game, and the actual player controlling said character. teamId can only have 1 of 2 values (constants).
Reply all
Reply to author
Forward
0 new messages