我对Python(和Django)比较陌生,并且在MySQL数据库中以行列值格式存储了一些数据
Django模型“ UserParameters”:
user_id | param | value
-----------------------------------
123 | param_1 | xxx
123 | param_2 | yyy
123 | param_3 | zzz
123 | param_4 | aaa
123 | param_5 | bbb
123 | param_6 | xxx
...
...
456 | param_1 | sse
456 | param_2 | aca
456 | param_3 | cce
456 | param_4 | dwe
456 | param_5 | cck
456 | param_6 | aq1
...
...
789 | param_1 | zzz
...
我正在尝试将MySQL中的数据以以下格式存储在字典中:
users = {
123: {'param_1': 'xxx', 'param_2': 'yyy', 'param_3': 'zzz', 'param_4': 'aaa', 'param_5': 'bbb', 'param_6': 'xxx'},
456: {'param_1': 'sse', 'param_2': 'aca', 'param_3': 'cce', 'param_4': 'dwe', 'param_5': 'cck', 'param_6': 'aq1'},
789: {'param_1': 'zzz', .....
}
到目前为止,我已经尝试过
users = {}
users_params = UserParameters.objects.all().values()
for u in users_params:
users[u['user_id']].add(u['param'], u['value'])
这给了我一个关键错误。知道我做错了什么吗?
谢谢!
user_params中的示例内容
{'id': 695, 'user_id': 5063, 'param': 'wk1_d2_survey_1', 'value': 768}
{'id': 708, 'user_id': 5063, 'param': 'wk3_d1_survey_2', 'value': 781}
{'id': 703, 'user_id': 5063, 'param': 'wk2_d2_survey_1', 'value': 776}
{'id': 720, 'user_id': 5063, 'param': 'wk14_d3_survey', 'value': 793}
{'id': 714, 'user_id': 5063, 'param': 'wk3_d2_survey_4', 'value': 787}
{'id': 696, 'user_id': 5063, 'param': 'wk1_d2_survey_2', 'value': 769}
{'id': 709, 'user_id': 5063, 'param': 'wk3_d1_survey_3', 'value': 782}
{'id': 691, 'user_id': 5063, 'param': 'wk1_d1_survey_1', 'value': 764}
{'id': 704, 'user_id': 5063, 'param': 'wk2_d2_survey_2', 'value': 777}
{'id': 699, 'user_id': 5063, 'param': 'wk2_d1_survey_1', 'value': 772}
{'id': 715, 'user_id': 5063, 'param': 'wk4_d1_survey', 'value': 788}
{'id': 697, 'user_id': 5063, 'param': 'wk1_d2_survey_3', 'value': 770}
{'id': 710, 'user_id': 5063, 'param': 'wk3_d1_survey_4', 'value': 783}
{'id': 692, 'user_id': 5063, 'param': 'wk1_d1_survey_2', 'value': 765}
{'id': 705, 'user_id': 5063, 'param': 'wk2_d2_survey_3', 'value': 778}
{'id': 700, 'user_id': 5063, 'param': 'wk2_d1_survey_2', 'value': 773}
{'id': 716, 'user_id': 5063, 'param': 'wk4_d2_survey', 'value': 789}
{'id': 698, 'user_id': 5063, 'param': 'wk1_d2_survey_4', 'value': 771}
{'id': 711, 'user_id': 5063, 'param': 'wk3_d2_survey_1', 'value': 784}
{'id': 693, 'user_id': 5063, 'param': 'wk1_d1_survey_3', 'value': 766}
{'id': 706, 'user_id': 5063, 'param': 'wk2_d2_survey_4', 'value': 779}
{'id': 701, 'user_id': 5063, 'param': 'wk2_d1_survey_3', 'value': 774}
{'id': 717, 'user_id': 5063, 'param': 'wk4_d3_survey', 'value': 790}
{'id': 718, 'user_id': 5063, 'param': 'wk14_d1_survey', 'value': 791}
{'id': 712, 'user_id': 5063, 'param': 'wk3_d2_survey_2', 'value': 785}
{'id': 694, 'user_id': 5063, 'param': 'wk1_d1_survey_4', 'value': 767}
{'id': 707, 'user_id': 5063, 'param': 'wk3_d1_survey_1', 'value': 780}
{'id': 702, 'user_id': 5063, 'param': 'wk2_d1_survey_4', 'value': 775}
{'id': 719, 'user_id': 5063, 'param': 'wk14_d2_survey', 'value': 792}
{'id': 713, 'user_id': 5063, 'param': 'wk3_d2_survey_3', 'value': 786}
{'id': 676, 'user_id': 5062, 'param': 'wk2_d2_survey_4', 'value': 749}
{'id': 663, 'user_id': 5062, 'param': 'wk1_d1_survey_3', 'value': 736}
{'id': 671, 'user_id': 5062, 'param': 'wk2_d1_survey_3', 'value': 744}
{'id': 687, 'user_id': 5062, 'param': 'wk4_d3_survey', 'value': 760}
{'id': 682, 'user_id': 5062, 'param': 'wk3_d2_survey_2', 'value': 755}
{'id': 688, 'user_id': 5062, 'param': 'wk14_d1_survey', 'value': 761}
{'id': 677, 'user_id': 5062, 'param': 'wk3_d1_survey_1', 'value': 750}
{'id': 664, 'user_id': 5062, 'param': 'wk1_d1_survey_4', 'value': 737}
{'id': 672, 'user_id': 5062, 'param': 'wk2_d1_survey_4', 'value': 745}
{'id': 683, 'user_id': 5062, 'param': 'wk3_d2_survey_3', 'value': 756}
{'id': 689, 'user_id': 5062, 'param': 'wk14_d2_survey', 'value': 762}
{'id': 678, 'user_id': 5062, 'param': 'wk3_d1_survey_2', 'value': 751}
{'id': 665, 'user_id': 5062, 'param': 'wk1_d2_survey_1', 'value': 738}
{'id': 673, 'user_id': 5062, 'param': 'wk2_d2_survey_1', 'value': 746}
{'id': 684, 'user_id': 5062, 'param': 'wk3_d2_survey_4', 'value': 757}
{'id': 690, 'user_id': 5062, 'param': 'wk14_d3_survey', 'value': 763}
{'id': 679, 'user_id': 5062, 'param': 'wk3_d1_survey_3', 'value': 752}
{'id': 666, 'user_id': 5062, 'param': 'wk1_d2_survey_2', 'value': 739}
{'id': 674, 'user_id': 5062, 'param': 'wk2_d2_survey_2', 'value': 747}
{'id': 661, 'user_id': 5062, 'param': 'wk1_d1_survey_1', 'value': 734}
{'id': 685, 'user_id': 5062, 'param': 'wk4_d1_survey', 'value': 758}
{'id': 669, 'user_id': 5062, 'param': 'wk2_d1_survey_1', 'value': 742}
{'id': 680, 'user_id': 5062, 'param': 'wk3_d1_survey_4', 'value': 753}
{'id': 667, 'user_id': 5062, 'param': 'wk1_d2_survey_3', 'value': 740}
{'id': 675, 'user_id': 5062, 'param': 'wk2_d2_survey_3', 'value': 748}
{'id': 662, 'user_id': 5062, 'param': 'wk1_d1_survey_2', 'value': 735}
{'id': 670, 'user_id': 5062, 'param': 'wk2_d1_survey_2', 'value': 743}
{'id': 686, 'user_id': 5062, 'param': 'wk4_d2_survey', 'value': 759}
{'id': 681, 'user_id': 5062, 'param': 'wk3_d2_survey_1', 'value': 754}
添加回溯错误
(venv_demo) $ python3 manage.py shell < test.py
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/mklimeck/venv_demo/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/mklimeck/venv_demo/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/mklimeck/venv_demo/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/mklimeck/venv_demo/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/Users/mklimeck/venv_demo/lib/python3.7/site-packages/django/core/management/commands/shell.py", line 92, in handle
exec(sys.stdin.read())
File "<string>", line 14, in <module>
KeyError: 5063
Models.py
class UserParameters(models.Model):
user_id = models.ForeignKey(Users, to_field='user_id', db_column='user_id', on_delete=models.PROTECT)
param = models.CharField(max_length=50)
value = models.CharField(max_length=50)
class Meta:
db_table = "user_parameters"
unique_together = ("user_id", "param")
答案 0 :(得分:1)