質問編集履歴

2

未だ解決に至っていない為、これまでの経緯含めて掲載いたしました。

2021/11/22 15:52

投稿

hasemma
hasemma

スコア2

test CHANGED
@@ -1 +1 @@
1
- Djangoフォーム複数保存行ないたい。
1
+ Djangoフォーム複数の初期値データ出力させたい。
test CHANGED
@@ -4,17 +4,25 @@
4
4
 
5
5
  Djangoのポートフォリオサイト作成として、
6
6
 
7
- 現在、サッカーゲームサイトの作成を目指して、プログラム作成を行っております。
7
+ サッカーゲームサイトの作成を目指して、プログラム作成を行っております。
8
+
9
+
10
+
8
-
11
+ こちらの質問を上げさせて頂いてから、
12
+
9
-
13
+ https://itc.tokyo/2021/05/14/2548/
14
+
10
-
15
+ こちらのHPを参考に軌道修正を掛けたのですが、
16
+
17
+ modelsのSkillクラスの「player_skill」に複数の値を入れている為、
18
+
19
+ エラーが発生しております。
20
+
21
+
22
+
11
- 現在、躓いている点として、
23
+ 解決法として、いい方法があれば、
12
-
13
- 選手ページを編集し、複数のフォームを一括で保存出来ないことで躓いており、
24
+
14
-
15
- 他にも問題点は多々あるのですが、
16
-
17
- まずは、複数フォームの一括保存が出来る方法をご教示頂けると幸いです。
25
+ ご教示頂けると幸いです。
18
26
 
19
27
 
20
28
 
@@ -26,13 +34,15 @@
26
34
 
27
35
 
28
36
 
29
- **「本サイトで解消法を探し、試した後のエラー内容」**
37
+ **「エラー内容」**
30
-
38
+
31
- ```
39
+ ```
40
+
32
-
41
+ File "/Users//MyPort/myvenv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1283, in build_filter
42
+
43
+ arg, value = filter_expr
44
+
33
- TemplateSyntaxError at /detail/1/edit/
45
+ ValueError: too many values to unpack (expected 2)
34
-
35
- django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '`' from 'request.player.pk`'
36
46
 
37
47
  ```
38
48
 
@@ -50,116 +60,30 @@
50
60
 
51
61
 
52
62
 
53
- <div class="card card-profile my-5 mx-auto">
54
-
55
- <div class="card-body">
56
-
57
- <h5 class="card-title text-center">選手プロフィール編集</h5>
58
-
59
- <form method="post" action="" enctype="multipart/form-data">
60
-
61
- {% csrf_token %}
62
-
63
- <table class="profile_table mb-4">
64
-
65
- <tbody>
66
-
67
- <tr>
68
-
69
- <th class="header">選手画像</th>
70
-
71
- <td class="data">{{ player_form.player_image }}</td>
72
-
73
- </tr>
74
-
75
- <tr>
76
-
77
- <th class="header">リリース日</th>
78
-
79
- <td class="data form_wrap form_wrap-2">
80
-
81
- {% render_field player_form.date_field class='form-control' placeholder='リリース日' %}
82
-
83
- </td>
84
-
85
- </tr>
86
-
87
- 〜〜省略〜〜
88
-
89
- </tbody>
90
-
91
-     </table>
92
-
93
- <h5 class="card-title text-center">能力編集</h5>
63
+ <h5 class="card-title text-center">スキル編集</h5>
94
64
 
95
65
  <table class="profile_table mb-4">
96
66
 
97
67
  <tbody>
98
68
 
69
+ {% for skill_item in skill_form %}
70
+
99
71
  <tr>
100
72
 
101
- <th class="header">オフェンセンス</th>
73
+ <th class="header">スキル</th>
102
74
 
103
75
  <td class="data form_wrap form_wrap-2">
104
76
 
105
- {% render_field ability_form.offense_sense class='form-control' placeholder='OFセンス' %}
77
+ {% render_field skill_item.player_skill class='form-control' placeholder='スキル入力' %}
106
78
 
107
79
  </td>
108
80
 
109
81
  </tr>
110
82
 
111
- 〜〜省略〜〜
83
+ {% endfor %}
112
84
 
113
85
  </tbody>
114
86
 
115
- </table>
116
-
117
-
118
-
119
- <h5 class="card-title text-center">フォーメーション編集</h5>
120
-
121
- <table class="profile_table mb-4">
122
-
123
- <tbody>
124
-
125
- <tr>
126
-
127
- <th class="header">フォーメーション画像</th>
128
-
129
- <td class="data">{{ formation_form.formation_images }}</td>
130
-
131
- </tr>
132
-
133
- </tbody>
134
-
135
- </table>
136
-
137
-        
138
-
139
- <h5 class="card-title text-center">スキル編集</h5>
140
-
141
- <table class="profile_table mb-4">
142
-
143
- <tbody>
144
-
145
- {% for skill_item in skill_form %}
146
-
147
- <tr>
148
-
149
- <th class="header">スキル</th>
150
-
151
- <td class="data form_wrap form_wrap-2">
152
-
153
- {% render_field skill_item.player_skill class='form-control' placeholder='スキル入力' %}
154
-
155
- </td>
156
-
157
- </tr>
158
-
159
- {% endfor %}
160
-
161
- </tbody>
162
-
163
87
   </table>
164
88
 
165
89
 
@@ -204,71 +128,97 @@
204
128
 
205
129
  ```
206
130
 
131
+ @login_required
132
+
207
- class PlayerEditView(LoginRequiredMixin, View):
133
+ def playerupdateview(request, pk):
208
-
209
- def get(self, request, *args, **kwargs):
134
+
210
-
211
- player_data = Player.objects.get(id=self.kwargs['pk'])
135
+ player = get_object_or_404(Player, pk=pk)
212
-
136
+
213
- ability_data = Ability.objects.get(id=self.kwargs['pk'])
137
+ ability = Ability.objects.filter(player=pk)[0]
214
-
138
+
215
- skill_data = Skill.objects.get(id=self.kwargs['pk'])
139
+ skill_query_set = Skill.objects.filter(player=pk)
140
+
216
-
141
+ skillobj_kwarg = skill_query_set.values()
142
+
217
- formation_data = Formation.objects.get(id=self.kwargs['pk'])
143
+ formation = Formation.objects.filter(player=pk)[0]
218
-
219
-
220
-
221
-           player_form = PlayerEditForm(
222
-
223
- request.POST or None,
224
-
225
- initial = {
226
-
227
- 'date_field' : player_data.date_field,
228
-
229
-                                  〜〜 省略 〜〜
230
-
231
- })
232
-
233
-
234
-
235
- ability_form = AbilityEditForm(
236
-
237
- request.POST or None,
238
-
239
- initial = {
240
-
241
- 'offense_sense' : ability_data.offense_sense,
242
-
243
- 〜〜省略〜〜
244
-
245
- })
246
-
247
-
248
-
249
- skill_form = SkillEditForm(
250
-
251
- request.POST or None,
252
-
253
- initial = {
254
-
255
- 'player_skill' : skill_data.player_skill
256
-
257
- })
258
144
 
259
145
 
260
146
 
261
- formation_form = FormationEditForm(
262
-
263
- request.POST or None,
147
+ playerobj_kwarg = {
148
+
264
-
149
+ 'date_field':player.date_field,'initial':player.initial,'maximum':player.maximum,'level':player.level,
150
+
151
+ 'player_name':player.player_name,'category':player.category,'position_category':player.position_category,'league_category':player.league_category,
152
+
153
+ 'country':player.country,'club':player.club,'age':player.age,'height':player.height,
154
+
155
+ 'dominant_foot':player.dominant_foot,'playstyle':player.playstyle,'player_image':player.player_image,
156
+
157
+ }
158
+
265
- initial = {
159
+ abilityobj_kwarg = {
160
+
266
-
161
+ 'offense_sense':ability.offense_sense,'ball_control':ability.ball_control,'dribble':ability.dribble,'ball_keep':ability.ball_keep,
162
+
163
+ 'grander_pass':ability.grander_pass,'fly_pass':ability.fly_pass,'determining_power':ability.determining_power,'heading':ability.heading,
164
+
165
+ 'place_kick':ability.place_kick,'curve':ability.curve,'speed':ability.speed,'instantaneous_power':ability.instantaneous_power,
166
+
167
+ 'kick_power':ability.kick_power,'jumping':ability.jumping,'physical_contact':ability.physical_contact,
168
+
169
+ 'body_control':ability.body_control,'physical_fitness':ability.physical_fitness,'defense_sense':ability.defense_sense,
170
+
171
+ 'take_the_ball':ability.take_the_ball,'aggressiveness':ability.aggressiveness,'gksense':ability.gksense,
172
+
173
+ 'catching':ability.catching,'clearing':ability.clearing,'cobraging':ability.cobraging,
174
+
175
+ 'deflectiveing':ability.deflectiveing,'reverse_foot_frequency':ability.reverse_foot_frequency,'reverse_foot_accuracy':ability.reverse_foot_accuracy,
176
+
177
+ 'condition_stability':ability.condition_stability,'injury_resistance':ability.injury_resistance,
178
+
179
+ }
180
+
181
+ formationobj_kwarg = {
182
+
267
- 'formation_images' : formation_data.formation_images
183
+ 'formation_images':formation.formation_images
268
-
184
+
269
- })
185
+ }
186
+
187
+
188
+
270
-
189
+ player_form = PlayerEditForm(request.POST or playerobj_kwarg)
190
+
271
-
191
+ ability_form = AbilityEditForm(request.POST or abilityobj_kwarg)
192
+
193
+ skill_form = SkillEditForm(request.POST or skillobj_kwarg)
194
+
195
+ formation_form = FormationEditForm(request.POST or formationobj_kwarg)
196
+
197
+
198
+
199
+ if player_form.is_valid() and ability_form.is_valid() and formation_form.is_valid() and skill_form.is_valid():
200
+
201
+ plyaer_data = player_form.save(commit=False)
202
+
203
+ plyaer_data.save()
204
+
205
+ ability_data = ability_form.save(commit=False)
206
+
207
+ ability_data.save()
208
+
209
+ formation_data = formation_form.save(commit=False)
210
+
211
+ formation_data.save()
212
+
213
+ skill_data = skill_form.save(commit=False)
214
+
215
+ skill_data.player = player
216
+
217
+ skill_data.save()
218
+
219
+ return redirect('detail', id=player.id)
220
+
221
+
272
222
 
273
223
  return render(request, 'pes21/detail_edit.html', {
274
224
 
@@ -280,84 +230,6 @@
280
230
 
281
231
  'formation_form' : formation_form
282
232
 
283
- })
284
-
285
-
286
-
287
- def post(self, request, *args, **kwargs):
288
-
289
- player_form = PlayerEditForm(request.POST or None)
290
-
291
- ability_form = AbilityEditForm(request.POST or None)
292
-
293
- formation_form = FormationEditForm(request.POST or None)
294
-
295
- skill_form = SkillEditForm(request.POST or None)
296
-
297
-
298
-
299
-
300
-
301
- if player_form.is_valid() and ability_form.is_valid() and formation_form.is_valid() and skill_form.is_valid():
302
-
303
- player_data = Player.objects.get(id=self.kwargs['pk'])
304
-
305
- ability_data = Ability.objects.get(id=self.kwargs['pk'])
306
-
307
- formation_data = Formation.objects.get(id=self.kwargs['pk'])
308
-
309
- skill_data = Skill.objects.get(player_data=self.kwargs['pk'])
310
-
311
-
312
-
313
- player_data.date_field = player_form.cleaned_data['date_field']
314
-
315
- 〜〜省略〜〜
316
-
317
-
318
-
319
- ability_data.offense_sense = ability_form.cleaned_data['offense_sense']
320
-
321
- 〜〜省略〜〜
322
-
323
-
324
-
325
- formation_data.formation_images = formation_form.cleaned_data['formation_images']
326
-
327
- if request.FILES.get('formation_images'):
328
-
329
- formation_data.formation_images = request.FILES.get('formation_images')
330
-
331
-
332
-
333
- skill_data.player_skill = skill_form.cleaned_data['player_skill']
334
-
335
-
336
-
337
- player_data.save()
338
-
339
- ability_data.save()
340
-
341
- formation_data.save()
342
-
343
- skill_data.save()
344
-
345
-
346
-
347
- return redirect('detail', self.kwargs['pk'])
348
-
349
-
350
-
351
- return render(request, 'pes21/detail_edit.html', {
352
-
353
- 'player_form' : player_form,
354
-
355
- 'ability_form' : ability_form,
356
-
357
- 'skill_form' : skill_form,
358
-
359
- 'formation_form' : formation_form
360
-
361
233
  })
362
234
 
363
235
 
@@ -366,33 +238,31 @@
366
238
 
367
239
 
368
240
 
369
- **urls.py**
241
+ **models.py**
242
+
370
-
243
+ ```ここに言語を入力
244
+
245
+ class Skill(models.Model):
246
+
247
+ player = models.ForeignKey(Player, on_delete=models.CASCADE)
248
+
249
+ player_skill = models.CharField('スキル入力', max_length=20, blank=True, null=True)
250
+
251
+
252
+
371
- ```
253
+ ```
372
-
254
+
255
+
256
+
373
- from django.urls import path
257
+ **forms.py**
374
-
258
+
375
- from pes21 import views
259
+ ```ここに言語を入力
376
-
260
+
377
- from .views import detailview
261
+ class SkillEditForm(forms.Form):
378
-
379
-
380
-
381
- urlpatterns = [
262
+
382
-
383
- path('detail/<int:pk>/', detailview, name='detail'),
263
+ player_skill = forms.CharField(label='スキル入力', max_length=20)
384
-
385
- path('detail/<int:pk>/edit/', views.PlayerEditView.as_view(), name='detail_edit'),
264
+
386
-
387
- path('category/<str:category>/', views.CategoryView.as_view(), name='category'),
388
-
389
- ]
390
-
391
-
392
-
393
- ```
265
+ ```
394
-
395
-
396
266
 
397
267
 
398
268
 
@@ -402,21 +272,51 @@
402
272
 
403
273
 
404
274
 
275
+ **forms.py**
276
+
405
- ```
277
+ ```
278
+
406
-
279
+ class SkillCreateForm(forms.ModelForm):
280
+
281
+
282
+
283
+ def __init__(self, *args, **kwargs):
284
+
285
+ super().__init__(*args, **kwargs)
286
+
287
+ for field in self.fields.values():
288
+
407
- <form method="post" action="{% url 'detail_edit' request.player.pk %}" enctype="multipart/form-data">
289
+ field.widget.attrs['class'] = 'form-control'
290
+
291
+
292
+
408
-
293
+ class Meta:
294
+
295
+ model = Skill
296
+
297
+ fields = '__all__'
298
+
299
+
300
+
301
+ SkillCreateFormSet = forms.modelformset_factory(
302
+
303
+ Skill, form=SkillCreateForm, extra=10)
304
+
409
- ```
305
+ ```
410
-
411
-
412
-
306
+
307
+
308
+
413
- detail_edit.htmlのformaction追加しましたが
309
+ modelformset_factory使って
414
-
310
+
415
- 発生している問題・エラーメッセージで表示されてるエラーが発生。
311
+ viewsの修正を試みまたが、使用方法をわかっていという点もあり、
312
+
416
-
313
+ 上手くいかず断念しております。
417
-
418
-
314
+
315
+
316
+
419
- 参考ページ:https://teratail.com/questions/243818
317
+ 参考:https://blog.narito.ninja/detail/30/
318
+
319
+
420
320
 
421
321
 
422
322
 

1

if player_form.is_valid()〜の表記で一部消されていた部分があった為、修正しております。

2021/11/22 15:52

投稿

hasemma
hasemma

スコア2

test CHANGED
File without changes
test CHANGED
@@ -298,7 +298,7 @@
298
298
 
299
299
 
300
300
 
301
- if player_form.is_valid() and ability_form.is_valid():
301
+ if player_form.is_valid() and ability_form.is_valid() and formation_form.is_valid() and skill_form.is_valid():
302
302
 
303
303
  player_data = Player.objects.get(id=self.kwargs['pk'])
304
304