简体   繁体   English

如果这个 django 表单本身也生成选项,为什么它不接受我的输入?

[英]Why doesn't this django form accept my input if it also generates the options itself?

It is a competition manager that matches the participants of a competition randomly, I call these pairs games.它是一个比赛经理,随机匹配比赛的参与者,我称之为配对游戏。 The game is a django model with an attribute called "ganador" to store the winner of the game.游戏是一个django model,带有一个名为“ganador”的属性来存储游戏的获胜者。 To choose the winner I use a modelform_factory called formaGanador and exclude all the attributes of the model except the "ganador" attribute.为了选择获胜者,我使用了一个名为 formaGanador 的 modelform_factory,并排除了 model 的所有属性,除了“ganador”属性。 The attribute "ganador" has an option of "choices" so that the form only allows to choose one of the participants of that game and not participants of other games.属性“ganador”具有“选择”选项,因此表单只允许选择该游戏的参与者之一,而不能选择其他游戏的参与者。 Finally, when I select a participant from the list and press the submit button on the form, I receive the following response: Select a valid choice.最后,当我从列表中的一个参与者 select 并按下表单上的提交按钮时,我收到以下响应:Select 一个有效的选择。 Player A is not one of the available choices.玩家 A 不是可用的选择之一。

model for games in model.py: model 用于 model.py 中的游戏:

class Juego(models.Model):
    torneo = models.ForeignKey(Torneo, on_delete=models.CASCADE, null=False)
    ronda = models.ForeignKey(Ronda, on_delete=models.CASCADE, null=False)
    jugadorA = models.ForeignKey(Jugador, on_delete=models.SET_NULL, null=True, related_name='jugadorA')
    jugadorB = models.ForeignKey(Jugador, on_delete=models.SET_NULL, null=True, related_name='jugadorB')
    puntuacionA = models.IntegerField(default=0)
    puntuacionB = models.IntegerField(default=0)
    ganador = models.ForeignKey(Jugador, on_delete=models.SET_NULL, null=True, default=None, choices=[('Jugador A', 'Jugador A'), ('Jugador B', 'Jugador B')])

creating the form in the views:在视图中创建表单:

GanadorForm = modelform_factory(Juego, exclude=['torneo', 'ronda', 'jugadorA', 'jugadorB', 'puntuacionA', 'puntuacionB'])

passing and receiving the form "formaganador" from the template:从模板传递和接收“formaganador”表格:

def detalleTorneo(request, id):
    torneo = Torneo.objects.get(pk=id)
    jugadoresPares = False
    jugadores = torneo.jugadores.all()
    no_jugadores = jugadores.count()
    rondas = Ronda.objects.filter(torneo=torneo)
    juegos = Juego.objects.filter(torneo=torneo)

    if request.method == 'POST':
        formaGanador = GanadorForm(request.POST)
        if formaGanador.is_valid():
            formaGanador.save()
    else:
        formaGanador = GanadorForm()

    if (no_jugadores % 2) == 0:
        jugadoresPares = True

    return render(request, 'torneos/detalle.html', {'juegos': juegos, 'torneo': torneo, 'no_jugadores': no_jugadores, 'jugadoresPares': jugadoresPares, 'rondas': rondas, 'formaGanador': formaGanador})

displaying the form "formaGanador" in the template:在模板中显示表单“formaGanador”:

    {% for ronda in rondas %}
        <h3>Ronda {{ronda.numero}}</h3>
        <ul>
            {% for juego in juegos %}
                {% if juego.ronda == ronda %}
            <li><form method="post"><strong>{{juego.jugadorA.nombre}} {{juego.jugadorA.apellido}} vs {{juego.jugadorB.nombre}} {{juego.jugadorB.apellido}}</strong> | {{formaGanador}} <button type="submit">Ingresar Resultados</button>{% csrf_token %}<br></form></li>
                {% endif %}
            {% endfor %}
        </ul>
    {% endfor %}

apologies for the variables in Spanish.为西班牙语中的变量道歉。

Django doesn't generate the options itself. Django 本身不会生成选项。 You do by setting the choices option in the genador field definition.您可以通过在 genador 字段定义中设置选项选项来实现。 The options you're specifying are invalid, because genador is a foreign key field and, therefore, needs a reference to a Jugador instance.您指定的选项无效,因为 genador 是外键字段,因此需要对 Jugador 实例的引用。 “Jugador A” is just a string not a Jugador instance. “Jugador A”只是一个字符串而不是 Jugador 实例。

Instead of restricting the choices in your model, you can implement a modelchoicefield in your form with a query set as needed ( docs ).无需限制 model 中的选择,您可以根据需要在表单中使用查询集 ( docs ) 实现modelchoicefield

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM