前提・実現したいこと
Djangoのポートフォリオサイト作成として、
サッカーゲームサイトの作成を目指して、プログラム作成を行っております。
こちらの質問を上げさせて頂いてから、
https://itc.tokyo/2021/05/14/2548/
こちらのHPを参考に軌道修正を掛けたのですが、
modelsのSkillクラスの「player_skill」に複数の値を入れている為、
エラーが発生しております。
解決法として、いい方法があれば、
ご教示頂けると幸いです。
宜しくお願い致します。
発生している問題・エラーメッセージ
「エラー内容」
File "/Users//MyPort/myvenv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1283, in build_filter arg, value = filter_expr ValueError: too many values to unpack (expected 2)
該当のソースコード
detail_edit.html
{% extends "pes21/base.html" %} {% load widget_tweaks %} {% block content %} <h5 class="card-title text-center">スキル編集</h5> <table class="profile_table mb-4"> <tbody> {% for skill_item in skill_form %} <tr> <th class="header">スキル</th> <td class="data form_wrap form_wrap-2"> {% render_field skill_item.player_skill class='form-control' placeholder='スキル入力' %} </td> </tr> {% endfor %} </tbody> </table> <div class="button mx-auto"> <button class="btn btn-1g btn-warning btn-block" type="submit"> 登録する </button> </div> </form> </div> </div> </form> </div> </div> {% endblock content %}
view.py
@login_required def playerupdateview(request, pk): player = get_object_or_404(Player, pk=pk) ability = Ability.objects.filter(player=pk)[0] skill_query_set = Skill.objects.filter(player=pk) skillobj_kwarg = skill_query_set.values() formation = Formation.objects.filter(player=pk)[0] playerobj_kwarg = { 'date_field':player.date_field,'initial':player.initial,'maximum':player.maximum,'level':player.level, 'player_name':player.player_name,'category':player.category,'position_category':player.position_category,'league_category':player.league_category, 'country':player.country,'club':player.club,'age':player.age,'height':player.height, 'dominant_foot':player.dominant_foot,'playstyle':player.playstyle,'player_image':player.player_image, } abilityobj_kwarg = { 'offense_sense':ability.offense_sense,'ball_control':ability.ball_control,'dribble':ability.dribble,'ball_keep':ability.ball_keep, 'grander_pass':ability.grander_pass,'fly_pass':ability.fly_pass,'determining_power':ability.determining_power,'heading':ability.heading, 'place_kick':ability.place_kick,'curve':ability.curve,'speed':ability.speed,'instantaneous_power':ability.instantaneous_power, 'kick_power':ability.kick_power,'jumping':ability.jumping,'physical_contact':ability.physical_contact, 'body_control':ability.body_control,'physical_fitness':ability.physical_fitness,'defense_sense':ability.defense_sense, 'take_the_ball':ability.take_the_ball,'aggressiveness':ability.aggressiveness,'gksense':ability.gksense, 'catching':ability.catching,'clearing':ability.clearing,'cobraging':ability.cobraging, 'deflectiveing':ability.deflectiveing,'reverse_foot_frequency':ability.reverse_foot_frequency,'reverse_foot_accuracy':ability.reverse_foot_accuracy, 'condition_stability':ability.condition_stability,'injury_resistance':ability.injury_resistance, } formationobj_kwarg = { 'formation_images':formation.formation_images } player_form = PlayerEditForm(request.POST or playerobj_kwarg) ability_form = AbilityEditForm(request.POST or abilityobj_kwarg) skill_form = SkillEditForm(request.POST or skillobj_kwarg) formation_form = FormationEditForm(request.POST or formationobj_kwarg) if player_form.is_valid() and ability_form.is_valid() and formation_form.is_valid() and skill_form.is_valid(): plyaer_data = player_form.save(commit=False) plyaer_data.save() ability_data = ability_form.save(commit=False) ability_data.save() formation_data = formation_form.save(commit=False) formation_data.save() skill_data = skill_form.save(commit=False) skill_data.player = player skill_data.save() return redirect('detail', id=player.id) return render(request, 'pes21/detail_edit.html', { 'player_form' : player_form, 'ability_form' : ability_form, 'skill_form' : skill_form, 'formation_form' : formation_form })
models.py
class Skill(models.Model): player = models.ForeignKey(Player, on_delete=models.CASCADE) player_skill = models.CharField('スキル入力', max_length=20, blank=True, null=True)
forms.py
class SkillEditForm(forms.Form): player_skill = forms.CharField(label='スキル入力', max_length=20)
試したこと
forms.py
class SkillCreateForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for field in self.fields.values(): field.widget.attrs['class'] = 'form-control' class Meta: model = Skill fields = '__all__' SkillCreateFormSet = forms.modelformset_factory( Skill, form=SkillCreateForm, extra=10)
modelformset_factoryを使って、
viewsの修正を試みましたが、使用方法をわかっていないという点もあり、
上手くいかず断念しております。
参考:https://blog.narito.ninja/detail/30/
補足情報(FW/ツールのバージョンなど)
使用環境:mac book air m1 VScode
あなたの回答
tips
プレビュー