在django模型上保存一个列表



大家好,我正在使用python, django和django rest框架我想把字典上的信息转换成模型但我不知道怎么做如果你能告诉我怎么做我将非常感谢

这是列表

[{'evolution': 'charizard',
'height': 11,
'id_pokemon': '5',
'name': 'charmeleon',
'stats': [[80, 'speed'],
[65, 'special-defense'],
[80, 'special-attack'],
[58, 'defense'],
[64, 'attack'],
[58, 'hp']],
'weight': 190},
{'evolution': 'charmeleon',
'height': 6,
'id_pokemon': '4',
'name': 'charmander',
'stats': [[65, 'speed'],
[50, 'special-defense'],
[60, 'special-attack'],
[43, 'defense'],
[52, 'attack'],
[39, 'hp']],
'weight': 85},
{'evolution': 'None',
'height': 17,
'id_pokemon': '6',
'name': 'charizard',
'stats': [[100, 'speed'],
[85, 'special-defense'],
[109, 'special-attack'],
[78, 'defense'],
[84, 'attack'],
[78, 'hp']],
'weight': 905}]
#_____________________________________
#And i want to save it on this model

class Pokemon(models.Model):
"""Database model for pokemon information"""
id_pokemon = models.IntegerField(primary_key=True)
name = models.CharField(max_length=255)
weight = models.IntegerField()
height = models.IntegerField()
special_defense = models.IntegerField()
special_attack = models.IntegerField()
defense = models.IntegerField()
attack = models.IntegerField()
hp = models.IntegerField()
speed = models.IntegerField()
evolution = models.IntegerField()

你可以调整你的数据如下,可以使用嵌套序列化器与两个模型PokemonStats一对一的关系。

[
{
'evolution': 'charizard',
'height': 11,
'id_pokemon': '5',
'name': 'charmeleon',
'stats': {
'speed': 80,
'special-defense': 65,
'special-attack': 80,
'defense': 58,
'attack': 64,
'hp': 58
},
'weight': 190
}
]

相关内容

  • 没有找到相关文章

最新更新