質問編集履歴

1

情報の修正

2017/09/11 12:20

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -2,160 +2,6 @@
2
2
 
3
3
  エクセルをパースしてその結果をUserモデルに結合させたいです。
4
4
 
5
- 結合の方法は、もうすでにUserモデルにデータが入っていて、同じuser_idを持ったデータに今さらに追加したいデータを追加させたいです。
6
-
7
- views.pyには
8
-
9
- ```ここに言語を入力
10
-
11
- #coding:utf-8
12
-
13
- from django.shortcuts import render
14
-
15
- import xlrd
16
-
17
- from .models import User
18
5
 
19
6
 
20
-
21
- book = xlrd.open_workbook('../data/excel1.xlsx')
22
-
23
- sheet = book.sheet_by_index(1)
24
-
25
-
26
-
27
- def build_employee(employee):
28
-
29
- if employee == 'leader':
30
-
31
- return 'l'
32
-
33
- if employee == 'manager':
34
-
35
- return 'm'
36
-
37
- if employee == 'others':
38
-
39
- return 'o'
40
-
41
-
42
-
43
- for row_index in range(sheet.nrows):
44
-
45
- rows = sheet.row_values(row_index)
46
-
47
- is_man = rows[4] != ""
48
-
49
- emp = build_employee(rows[5])
50
-
51
- user = User(user_id=rows[1], name_id=rows[2], name=rows[3],
52
-
53
- age=rows[4],man=is_man,employee=emp)
54
-
55
- user.save()
56
-
57
-
58
-
59
-
60
-
61
- files = glob.glob('./user/*.xlsx')
62
-
63
-
64
-
65
- data_dict_key ={}
66
-
67
- for x in files:
68
-
69
- if "$" not in x:
70
-
71
- book3 = xlrd.open_workbook(x)
72
-
73
- sheet3 = book3.sheet_by_index(0)
74
-
75
- cells = [
76
-
77
- ('company_id', 0, 4),
78
-
79
- ('user_id', 0, 5),
80
-
81
- ('name', 0, 6),
82
-
83
- ]
84
-
85
- data_dict = OrderedDict()
86
-
87
- for key, rowy, colx in cells:
88
-
89
- try:
90
-
91
- data_dict[key] = sheet3.cell_value(rowy, colx)
92
-
93
- except IndexError:
94
-
95
- data_dict[key] = None
96
-
97
-
98
-
99
- if data_dict['user_id'] in data_dict_key:
100
-
101
-
102
-
103
- data_dict_key[data_dict['user_id']].update(data_dict)
104
-
105
- continue
106
-
107
- data_dict[data_dict_key['user_id']] = data_dict
108
-
109
- for row_number, row_data in data_dict_key.items():
110
-
111
- user1 = User.filter(user_id=row_data['user_id']).exists()
112
-
113
- if user1:
114
-
115
- user1.__dict__.update(**data_dict_key)
116
-
117
- user1.save()
118
-
119
- ```
120
-
121
- と書きました。
122
-
123
- db.sqlite3 はここで加えたデータと
124
-
125
- ```ここに言語を入力
126
-
127
- User(user_id=rows[1], name_id=rows[2], name=rows[3],
128
-
129
- age=rows[4],man=is_man,employee=emp)
130
-
131
- ```
132
-
133
- ここで加えたデータ
134
-
135
- ```ここに言語を入力
136
-
137
- user1.__dict__.update(**data_dict_key)
138
-
139
- ```
140
-
141
- を別々に持っていますuser_idごとに2つのデータが紐づいていません。models.pyには
142
-
143
- ```ここに言語を入力
144
-
145
- #coding:utf-8
146
-
147
- from django.db import models
148
-
149
- class User(models.Model):
150
-
151
- company_id = models.CharField(max_length=200)
152
-
153
- user_id = models.CharField(max_length=200)
154
-
155
- name = models.CharField(max_length=200)
156
-
157
- ```
158
-
159
- と書きました。
160
-
161
7
  エラーは何も出ていません。どう直せば良いでしょうか?