質問編集履歴
2
mixins追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -200,6 +200,28 @@
|
|
200
200
|
|
201
201
|
|
202
202
|
|
203
|
+
def get_week_calendar(self):
|
204
|
+
|
205
|
+
calendar_context = super().get_week_calendar()
|
206
|
+
|
207
|
+
calendar_context['df'] = self.get_week_schedules(
|
208
|
+
|
209
|
+
calendar_context['week_first'],
|
210
|
+
|
211
|
+
calendar_context['week_last'],
|
212
|
+
|
213
|
+
calendar_context['week_days']
|
214
|
+
|
215
|
+
)
|
216
|
+
|
217
|
+
return calendar_context
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
203
225
|
```
|
204
226
|
|
205
227
|
|
@@ -236,6 +258,8 @@
|
|
236
258
|
|
237
259
|
return context
|
238
260
|
|
261
|
+
|
262
|
+
|
239
263
|
```
|
240
264
|
|
241
265
|
|
1
コメントに対しての追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -31,3 +31,227 @@
|
|
31
31
|
調べたところあまりdjangoでpandasで出力している記事がなく(英語は分かりません)情報が少なく行き詰ってます。
|
32
32
|
|
33
33
|
よろしくお願いします。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
追記
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
```
|
44
|
+
|
45
|
+
response = HttpResponse(content_type='text/csv')
|
46
|
+
|
47
|
+
response['Content-Disposition'] = 'attachment; filename=a.csv'
|
48
|
+
|
49
|
+
df.to_csv(path_or_buf=response,sep=';',float_format='%.2f',index=False,decimal=",")
|
50
|
+
|
51
|
+
```
|
52
|
+
|
53
|
+
上記試しましたが上手くいきませんでした。
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
mixins.pyでcsv出力をしています。(views.pyだと上手く行かなかった)
|
58
|
+
|
59
|
+
だから上手く行かないのでしょうか?
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
mixins.py
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
class CsvMixin(Week_CsvMixin):
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
def get_week_schedules(self, start, end, days):
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
shop = get_object_or_404(Shops, pk=self.kwargs['shops_pk'])
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
user= User.objects.filter(shops__shop=shop)
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
b =[]
|
86
|
+
|
87
|
+
for a in user:
|
88
|
+
|
89
|
+
b.append(a)
|
90
|
+
|
91
|
+
lookup = {
|
92
|
+
|
93
|
+
'{}__range'.format(self.date_field): (start, end),
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
queryset = self.model.objects.filter(**lookup)
|
100
|
+
|
101
|
+
days = {day: [] for day in days}
|
102
|
+
|
103
|
+
df = pd.DataFrame(days)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
a=1
|
108
|
+
|
109
|
+
for schedule in queryset:
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
if schedule.user in b:
|
114
|
+
|
115
|
+
if a == 1:
|
116
|
+
|
117
|
+
user=schedule.user.last_name+' '+schedule.user.first_name
|
118
|
+
|
119
|
+
date= schedule.date
|
120
|
+
|
121
|
+
start_time=schedule.get_start_time_display()
|
122
|
+
|
123
|
+
end_time = schedule.get_end_time_display()
|
124
|
+
|
125
|
+
time = start_time+'-'+end_time
|
126
|
+
|
127
|
+
ddf =pd.DataFrame({date:time},index =[user])
|
128
|
+
|
129
|
+
df = pd.concat([df,ddf],axis=0)
|
130
|
+
|
131
|
+
df.fillna(" ", inplace=True)
|
132
|
+
|
133
|
+
a = 2
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
if user != schedule.user.last_name+' '+schedule.user.first_name:
|
138
|
+
|
139
|
+
user=schedule.user.last_name+' '+schedule.user.first_name
|
140
|
+
|
141
|
+
date= schedule.date
|
142
|
+
|
143
|
+
start_time=schedule.get_start_time_display()
|
144
|
+
|
145
|
+
end_time = schedule.get_end_time_display()
|
146
|
+
|
147
|
+
time = start_time+'-'+end_time
|
148
|
+
|
149
|
+
ddf =pd.DataFrame({date:time},index =[user])
|
150
|
+
|
151
|
+
df = pd.concat([df,ddf],axis=0)
|
152
|
+
|
153
|
+
df.fillna(" ", inplace=True)
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
else:
|
158
|
+
|
159
|
+
user=schedule.user.last_name+' '+schedule.user.first_name
|
160
|
+
|
161
|
+
date= schedule.date
|
162
|
+
|
163
|
+
start_time=schedule.get_start_time_display()
|
164
|
+
|
165
|
+
end_time = schedule.get_end_time_display()
|
166
|
+
|
167
|
+
time = start_time+'-'+end_time
|
168
|
+
|
169
|
+
ddf =pd.DataFrame({date:time},index =[user])
|
170
|
+
|
171
|
+
df[date]= df[date].astype(str)
|
172
|
+
|
173
|
+
df.at[user,date] =time
|
174
|
+
|
175
|
+
df.fillna(" ", inplace=True)
|
176
|
+
|
177
|
+
# csv保存-------------------
|
178
|
+
|
179
|
+
df.fillna(" ", inplace=True)
|
180
|
+
|
181
|
+
today = datetime.datetime.today()
|
182
|
+
|
183
|
+
day = today.strftime("%Y%m%d")
|
184
|
+
|
185
|
+
# df.to_csv(day + '_list.csv',encoding= "utf_8_sig")←動作はするが、manage.pyと同じ場所に保存される
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
↓エラーは出ないが出力はされない
|
190
|
+
|
191
|
+
response = HttpResponse(content_type='text/csv')
|
192
|
+
|
193
|
+
response['Content-Disposition'] = 'attachment; filename=a.csv'
|
194
|
+
|
195
|
+
df.to_csv(path_or_buf=response,sep=';',float_format='%.2f',index=False,decimal=",")
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
return df
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
```
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
```
|
208
|
+
|
209
|
+
views.py
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
class Shift_csv(mixins.CsvMixin, generic.TemplateView):
|
214
|
+
|
215
|
+
model = Schedule
|
216
|
+
|
217
|
+
template_name = 'shift/shift_csv.html'
|
218
|
+
|
219
|
+
date_field = 'date'
|
220
|
+
|
221
|
+
def get_context_data(self, **kwargs):
|
222
|
+
|
223
|
+
context = super().get_context_data(**kwargs)
|
224
|
+
|
225
|
+
shop = get_object_or_404(Shops, pk=self.kwargs['shops_pk'])
|
226
|
+
|
227
|
+
context['shops']= User.objects.filter(shops__shop=shop)
|
228
|
+
|
229
|
+
context['shopnum']=self.kwargs['shops_pk']
|
230
|
+
|
231
|
+
calendar_context = self.get_week_calendar()
|
232
|
+
|
233
|
+
context.update(calendar_context)
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
return context
|
238
|
+
|
239
|
+
```
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
views.pyの書き方的に多分変なんだと思いますが、強引にやりました。
|
244
|
+
|
245
|
+
viewsでcsv出力のcodeを書くと上手く行きませんでした。
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
df =context['df']
|
250
|
+
|
251
|
+
df.to_csv('a.csv')
|
252
|
+
|
253
|
+
```
|
254
|
+
|
255
|
+
みたいに書きましたが上手く動作せず、結果mixinsで書く感じになってます。
|
256
|
+
|
257
|
+
出力されるならどこに書いてもいいんですが、、。
|