質問編集履歴

2

修正

2018/07/17 23:31

投稿

nokonoko_1203
nokonoko_1203

スコア17

test CHANGED
File without changes
test CHANGED
@@ -362,13 +362,13 @@
362
362
 
363
363
 
364
364
 
365
- for cleaned_data in form.cleaned_data:
365
+ for f in form:
366
-
366
+
367
- Purchase_history.objects.create(name=cleaned_data["name"],
367
+ Purchase_history.objects.create(name=f.cleaned_data["name"],
368
-
368
+
369
- price=cleaned_data["price"])
369
+ price=f.cleaned_data["price"])
370
-
371
-
370
+
371
+
372
372
 
373
373
  return super().form_valid(form)
374
374
 
@@ -437,3 +437,33 @@
437
437
  fields = ("name", "price")
438
438
 
439
439
  ```
440
+
441
+
442
+
443
+ ``` Local vars
444
+
445
+ __class__
446
+
447
+ <class 'stockpile.views.Shopping_listCreateView'>
448
+
449
+
450
+
451
+ f
452
+
453
+ <Shopping_listForm bound=True, valid=True, fields=(name;price;id;DELETE)>
454
+
455
+
456
+
457
+ form
458
+
459
+ <django.forms.formsets.Shopping_listFormFormSet object at 0x7f1e95ec36a0>
460
+
461
+
462
+
463
+ self
464
+
465
+ <stockpile.views.Shopping_listCreateView object at 0x7f1e95df7518>
466
+
467
+ ```
468
+
469
+ fオブジェクトにはnameが存在しているように見えるのですが、エラーとなります

1

修正したコードを追記

2018/07/17 23:30

投稿

nokonoko_1203
nokonoko_1203

スコア17

test CHANGED
File without changes
test CHANGED
@@ -275,3 +275,165 @@
275
275
 
276
276
 
277
277
  で、実装できるのかな。と考えておりましたが、そもそもそのやりかたも分からず、ご教授頂きたいです。
278
+
279
+
280
+
281
+ 追記:修正しましたが解決に至っておりません。コードは下記の通りです。
282
+
283
+ ```python
284
+
285
+ # views.py
286
+
287
+
288
+
289
+ # Shopping_listView
290
+
291
+
292
+
293
+ class Shopping_listView(ListView):
294
+
295
+ model = Shopping_list
296
+
297
+ paginate_by = 30
298
+
299
+ template_name = "stockpile/shopping_list.html"
300
+
301
+ queryset = Shopping_list.objects.all()
302
+
303
+
304
+
305
+ def get_context_data(self, **kwargs):
306
+
307
+ context = super().get_context_data(**kwargs)
308
+
309
+ '''
310
+
311
+ 辞書のリストが帰ってくる
312
+
313
+ [{"id": 1, "price":500......}]
314
+
315
+ '''
316
+
317
+ all_list_price = Shopping_list.objects.all().values("id", "price")
318
+
319
+ sum_price = 0
320
+
321
+ for v in all_list_price:
322
+
323
+ price = v["price"]
324
+
325
+ sum_price += price
326
+
327
+ context['sum_price'] = sum_price
328
+
329
+ return context
330
+
331
+
332
+
333
+
334
+
335
+ class Shopping_listCreateView(FormView):
336
+
337
+ # model = Shopping_list
338
+
339
+ # fields = ["name", "price"]
340
+
341
+ form_class = Shopping_listFormSet
342
+
343
+ template_name = "stockpile/shopping_list_form.html"
344
+
345
+ success_url = "/stockpile/shopping_list/"
346
+
347
+
348
+
349
+ def get_context_data(self, **kwargs):
350
+
351
+ context = super().get_context_data(**kwargs)
352
+
353
+ context['Week_menu'] = Week_menu.objects.all()
354
+
355
+ return context
356
+
357
+
358
+
359
+ def form_valid(self, form):
360
+
361
+ form.save() # ここで保存
362
+
363
+
364
+
365
+ for cleaned_data in form.cleaned_data:
366
+
367
+ Purchase_history.objects.create(name=cleaned_data["name"],
368
+
369
+ price=cleaned_data["price"])
370
+
371
+
372
+
373
+ return super().form_valid(form)
374
+
375
+
376
+
377
+
378
+
379
+ def delete_shopping_list(request, shopping_list_id):
380
+
381
+ shopping_list = get_object_or_404(Shopping_list, id=shopping_list_id)
382
+
383
+ shopping_list.delete()
384
+
385
+ shopping_list = Shopping_list.objects.all()
386
+
387
+ context = {'object_list': shopping_list}
388
+
389
+ return render(request, 'stockpile/shopping_list.html', context)
390
+
391
+ ```
392
+
393
+ ```python
394
+
395
+ forms.py
396
+
397
+
398
+
399
+ class Shopping_listForm(ModelForm):
400
+
401
+
402
+
403
+ class Meta:
404
+
405
+ model = Shopping_list
406
+
407
+ fields = ("name", "price")
408
+
409
+
410
+
411
+
412
+
413
+ Shopping_listFormSet = modelformset_factory(
414
+
415
+ Shopping_list,
416
+
417
+ form=Shopping_listForm,
418
+
419
+ fields=("name", "price"),
420
+
421
+ extra=3,
422
+
423
+ can_delete=True
424
+
425
+ )
426
+
427
+
428
+
429
+ class Purchase_historyForm(ModelForm):
430
+
431
+
432
+
433
+ class Meta:
434
+
435
+ model = Purchase_history
436
+
437
+ fields = ("name", "price")
438
+
439
+ ```