質問編集履歴
2
未だ解決に至っていない為、これまでの経緯含めて掲載いたしました。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Djangoフォーム
|
1
|
+
Djangoフォームで複数の初期値データを出力させたい。
|
body
CHANGED
@@ -1,21 +1,26 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
3
|
Djangoのポートフォリオサイト作成として、
|
4
|
-
|
4
|
+
サッカーゲームサイトの作成を目指して、プログラム作成を行っております。
|
5
5
|
|
6
|
-
|
6
|
+
こちらの質問を上げさせて頂いてから、
|
7
|
-
|
7
|
+
https://itc.tokyo/2021/05/14/2548/
|
8
|
-
|
8
|
+
こちらのHPを参考に軌道修正を掛けたのですが、
|
9
|
-
|
9
|
+
modelsのSkillクラスの「player_skill」に複数の値を入れている為、
|
10
|
+
エラーが発生しております。
|
10
11
|
|
12
|
+
解決法として、いい方法があれば、
|
13
|
+
ご教示頂けると幸いです。
|
14
|
+
|
11
15
|
宜しくお願い致します。
|
12
16
|
|
13
17
|
### 発生している問題・エラーメッセージ
|
14
18
|
|
15
|
-
**「
|
19
|
+
**「エラー内容」**
|
16
20
|
```
|
21
|
+
File "/Users//MyPort/myvenv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1283, in build_filter
|
22
|
+
arg, value = filter_expr
|
17
|
-
|
23
|
+
ValueError: too many values to unpack (expected 2)
|
18
|
-
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '`' from 'request.player.pk`'
|
19
24
|
```
|
20
25
|
|
21
26
|
### 該当のソースコード
|
@@ -24,49 +29,6 @@
|
|
24
29
|
```
|
25
30
|
{% extends "pes21/base.html" %} {% load widget_tweaks %} {% block content %}
|
26
31
|
|
27
|
-
<div class="card card-profile my-5 mx-auto">
|
28
|
-
<div class="card-body">
|
29
|
-
<h5 class="card-title text-center">選手プロフィール編集</h5>
|
30
|
-
<form method="post" action="" enctype="multipart/form-data">
|
31
|
-
{% csrf_token %}
|
32
|
-
<table class="profile_table mb-4">
|
33
|
-
<tbody>
|
34
|
-
<tr>
|
35
|
-
<th class="header">選手画像</th>
|
36
|
-
<td class="data">{{ player_form.player_image }}</td>
|
37
|
-
</tr>
|
38
|
-
<tr>
|
39
|
-
<th class="header">リリース日</th>
|
40
|
-
<td class="data form_wrap form_wrap-2">
|
41
|
-
{% render_field player_form.date_field class='form-control' placeholder='リリース日' %}
|
42
|
-
</td>
|
43
|
-
</tr>
|
44
|
-
〜〜省略〜〜
|
45
|
-
</tbody>
|
46
|
-
</table>
|
47
|
-
<h5 class="card-title text-center">能力編集</h5>
|
48
|
-
<table class="profile_table mb-4">
|
49
|
-
<tbody>
|
50
|
-
<tr>
|
51
|
-
<th class="header">オフェンスセンス</th>
|
52
|
-
<td class="data form_wrap form_wrap-2">
|
53
|
-
{% render_field ability_form.offense_sense class='form-control' placeholder='OFセンス' %}
|
54
|
-
</td>
|
55
|
-
</tr>
|
56
|
-
〜〜省略〜〜
|
57
|
-
</tbody>
|
58
|
-
</table>
|
59
|
-
|
60
|
-
<h5 class="card-title text-center">フォーメーション編集</h5>
|
61
|
-
<table class="profile_table mb-4">
|
62
|
-
<tbody>
|
63
|
-
<tr>
|
64
|
-
<th class="header">フォーメーション画像</th>
|
65
|
-
<td class="data">{{ formation_form.formation_images }}</td>
|
66
|
-
</tr>
|
67
|
-
</tbody>
|
68
|
-
</table>
|
69
|
-
|
70
32
|
<h5 class="card-title text-center">スキル編集</h5>
|
71
33
|
<table class="profile_table mb-4">
|
72
34
|
<tbody>
|
@@ -101,77 +63,51 @@
|
|
101
63
|
|
102
64
|
**view.py**
|
103
65
|
```
|
66
|
+
@login_required
|
104
|
-
|
67
|
+
def playerupdateview(request, pk):
|
105
|
-
def get(self, request, *args, **kwargs):
|
106
|
-
|
68
|
+
player = get_object_or_404(Player, pk=pk)
|
107
|
-
|
69
|
+
ability = Ability.objects.filter(player=pk)[0]
|
108
|
-
|
70
|
+
skill_query_set = Skill.objects.filter(player=pk)
|
71
|
+
skillobj_kwarg = skill_query_set.values()
|
109
|
-
|
72
|
+
formation = Formation.objects.filter(player=pk)[0]
|
110
|
-
|
111
|
-
player_form = PlayerEditForm(
|
112
|
-
request.POST or None,
|
113
|
-
initial = {
|
114
|
-
'date_field' : player_data.date_field,
|
115
|
-
〜〜 省略 〜〜
|
116
|
-
})
|
117
|
-
|
118
|
-
ability_form = AbilityEditForm(
|
119
|
-
request.POST or None,
|
120
|
-
initial = {
|
121
|
-
'offense_sense' : ability_data.offense_sense,
|
122
|
-
〜〜省略〜〜
|
123
|
-
})
|
124
|
-
|
125
|
-
skill_form = SkillEditForm(
|
126
|
-
request.POST or None,
|
127
|
-
initial = {
|
128
|
-
'player_skill' : skill_data.player_skill
|
129
|
-
})
|
130
73
|
|
131
|
-
formation_form = FormationEditForm(
|
132
|
-
|
74
|
+
playerobj_kwarg = {
|
75
|
+
'date_field':player.date_field,'initial':player.initial,'maximum':player.maximum,'level':player.level,
|
76
|
+
'player_name':player.player_name,'category':player.category,'position_category':player.position_category,'league_category':player.league_category,
|
77
|
+
'country':player.country,'club':player.club,'age':player.age,'height':player.height,
|
78
|
+
'dominant_foot':player.dominant_foot,'playstyle':player.playstyle,'player_image':player.player_image,
|
79
|
+
}
|
133
|
-
|
80
|
+
abilityobj_kwarg = {
|
81
|
+
'offense_sense':ability.offense_sense,'ball_control':ability.ball_control,'dribble':ability.dribble,'ball_keep':ability.ball_keep,
|
82
|
+
'grander_pass':ability.grander_pass,'fly_pass':ability.fly_pass,'determining_power':ability.determining_power,'heading':ability.heading,
|
83
|
+
'place_kick':ability.place_kick,'curve':ability.curve,'speed':ability.speed,'instantaneous_power':ability.instantaneous_power,
|
84
|
+
'kick_power':ability.kick_power,'jumping':ability.jumping,'physical_contact':ability.physical_contact,
|
85
|
+
'body_control':ability.body_control,'physical_fitness':ability.physical_fitness,'defense_sense':ability.defense_sense,
|
86
|
+
'take_the_ball':ability.take_the_ball,'aggressiveness':ability.aggressiveness,'gksense':ability.gksense,
|
87
|
+
'catching':ability.catching,'clearing':ability.clearing,'cobraging':ability.cobraging,
|
88
|
+
'deflectiveing':ability.deflectiveing,'reverse_foot_frequency':ability.reverse_foot_frequency,'reverse_foot_accuracy':ability.reverse_foot_accuracy,
|
89
|
+
'condition_stability':ability.condition_stability,'injury_resistance':ability.injury_resistance,
|
90
|
+
}
|
91
|
+
formationobj_kwarg = {
|
134
|
-
|
92
|
+
'formation_images':formation.formation_images
|
135
|
-
|
93
|
+
}
|
136
94
|
|
137
|
-
return render(request, 'pes21/detail_edit.html', {
|
138
|
-
'player_form' : player_form,
|
139
|
-
'ability_form' : ability_form,
|
140
|
-
'skill_form' : skill_form,
|
141
|
-
'formation_form' : formation_form
|
142
|
-
})
|
143
|
-
|
144
|
-
def post(self, request, *args, **kwargs):
|
145
|
-
player_form = PlayerEditForm(request.POST or
|
95
|
+
player_form = PlayerEditForm(request.POST or playerobj_kwarg)
|
146
|
-
ability_form = AbilityEditForm(request.POST or
|
96
|
+
ability_form = AbilityEditForm(request.POST or abilityobj_kwarg)
|
147
|
-
formation_form = FormationEditForm(request.POST or None)
|
148
|
-
skill_form = SkillEditForm(request.POST or
|
97
|
+
skill_form = SkillEditForm(request.POST or skillobj_kwarg)
|
149
|
-
|
98
|
+
formation_form = FormationEditForm(request.POST or formationobj_kwarg)
|
150
99
|
|
151
100
|
if player_form.is_valid() and ability_form.is_valid() and formation_form.is_valid() and skill_form.is_valid():
|
152
|
-
|
101
|
+
plyaer_data = player_form.save(commit=False)
|
153
|
-
ability_data = Ability.objects.get(id=self.kwargs['pk'])
|
154
|
-
formation_data = Formation.objects.get(id=self.kwargs['pk'])
|
155
|
-
skill_data = Skill.objects.get(player_data=self.kwargs['pk'])
|
156
|
-
|
157
|
-
player_data.date_field = player_form.cleaned_data['date_field']
|
158
|
-
〜〜省略〜〜
|
159
|
-
|
160
|
-
ability_data.offense_sense = ability_form.cleaned_data['offense_sense']
|
161
|
-
〜〜省略〜〜
|
162
|
-
|
163
|
-
formation_data.formation_images = formation_form.cleaned_data['formation_images']
|
164
|
-
if request.FILES.get('formation_images'):
|
165
|
-
formation_data.formation_images = request.FILES.get('formation_images')
|
166
|
-
|
167
|
-
skill_data.player_skill = skill_form.cleaned_data['player_skill']
|
168
|
-
|
169
|
-
|
102
|
+
plyaer_data.save()
|
103
|
+
ability_data = ability_form.save(commit=False)
|
170
104
|
ability_data.save()
|
105
|
+
formation_data = formation_form.save(commit=False)
|
171
106
|
formation_data.save()
|
107
|
+
skill_data = skill_form.save(commit=False)
|
108
|
+
skill_data.player = player
|
172
109
|
skill_data.save()
|
173
|
-
|
174
|
-
return redirect('detail',
|
110
|
+
return redirect('detail', id=player.id)
|
175
111
|
|
176
112
|
return render(request, 'pes21/detail_edit.html', {
|
177
113
|
'player_form' : player_form,
|
@@ -182,33 +118,47 @@
|
|
182
118
|
|
183
119
|
```
|
184
120
|
|
185
|
-
**
|
121
|
+
**models.py**
|
122
|
+
```ここに言語を入力
|
123
|
+
class Skill(models.Model):
|
124
|
+
player = models.ForeignKey(Player, on_delete=models.CASCADE)
|
125
|
+
player_skill = models.CharField('スキル入力', max_length=20, blank=True, null=True)
|
126
|
+
|
186
127
|
```
|
187
|
-
from django.urls import path
|
188
|
-
from pes21 import views
|
189
|
-
from .views import detailview
|
190
128
|
|
191
|
-
|
129
|
+
**forms.py**
|
130
|
+
```ここに言語を入力
|
131
|
+
class SkillEditForm(forms.Form):
|
192
|
-
|
132
|
+
player_skill = forms.CharField(label='スキル入力', max_length=20)
|
193
|
-
path('detail/<int:pk>/edit/', views.PlayerEditView.as_view(), name='detail_edit'),
|
194
|
-
path('category/<str:category>/', views.CategoryView.as_view(), name='category'),
|
195
|
-
]
|
196
|
-
|
197
133
|
```
|
198
134
|
|
199
135
|
|
200
|
-
|
201
136
|
### 試したこと
|
202
137
|
|
138
|
+
**forms.py**
|
203
139
|
```
|
140
|
+
class SkillCreateForm(forms.ModelForm):
|
141
|
+
|
142
|
+
def __init__(self, *args, **kwargs):
|
143
|
+
super().__init__(*args, **kwargs)
|
144
|
+
for field in self.fields.values():
|
204
|
-
|
145
|
+
field.widget.attrs['class'] = 'form-control'
|
146
|
+
|
147
|
+
class Meta:
|
148
|
+
model = Skill
|
149
|
+
fields = '__all__'
|
150
|
+
|
151
|
+
SkillCreateFormSet = forms.modelformset_factory(
|
152
|
+
Skill, form=SkillCreateForm, extra=10)
|
205
153
|
```
|
206
154
|
|
207
|
-
|
155
|
+
modelformset_factoryを使って、
|
208
|
-
|
156
|
+
viewsの修正を試みましたが、使用方法をわかっていないという点もあり、
|
157
|
+
上手くいかず断念しております。
|
209
158
|
|
210
|
-
参考
|
159
|
+
参考:https://blog.narito.ninja/detail/30/
|
211
160
|
|
161
|
+
|
212
162
|
### 補足情報(FW/ツールのバージョンなど)
|
213
163
|
|
214
164
|
使用環境:mac book air m1 VScode
|
1
if player_form.is_valid()〜の表記で一部消されていた部分があった為、修正しております。
title
CHANGED
File without changes
|
body
CHANGED
@@ -148,7 +148,7 @@
|
|
148
148
|
skill_form = SkillEditForm(request.POST or None)
|
149
149
|
|
150
150
|
|
151
|
-
if player_form.is_valid() and ability_form.is_valid():
|
151
|
+
if player_form.is_valid() and ability_form.is_valid() and formation_form.is_valid() and skill_form.is_valid():
|
152
152
|
player_data = Player.objects.get(id=self.kwargs['pk'])
|
153
153
|
ability_data = Ability.objects.get(id=self.kwargs['pk'])
|
154
154
|
formation_data = Formation.objects.get(id=self.kwargs['pk'])
|