0

I want to limit the options of 'ganador', which is a foreign key to the Jugador model, to the foreign keys of the 'jugadorA' and 'jugadorB' fields, which also point to the Jugador model. I guess I need to somehow access the instance of the Game model the form was created with in order to do a proper query set.

the model in models.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')
    ganador = models.ForeignKey(Jugador, on_delete=models.SET_NULL, null=True, default=None, related_name='ganador')

the form in forms.py:

class ganadorForm(ModelForm):

    class Meta:
        model = Juego
        fields = ['ganador', 'jugadorA', 'jugadorB', 'torneo', 'ronda']
        widgets = {
            'ganador': ModelChoiceField(queryset=Jugador.objects.filter(Q(fields['jugadorA']) | Q(fields['jugadorB'])), to_field_name='nombre')
        }

In the views, this is how I create the form (It's in a modelformset):

GanadorFormSet = modelformset_factory(Juego, fields=('ganador',), form=ganadorForm)

and this is where I use the formset in the view:

        if request.method == 'POST':
            formSet = GanadorFormSet(request.POST, request.FILES, queryset=juegosUltimaRonda)
            if formSet.is_valid():
                formSet.save()
                actualizarPuntuaciones(torneo_id=id)
                nuevaRonda(request=request, id=id)
                return redirect(f'/detalle_torneo/{id}')
        else:
            formSet = GanadorFormSet(queryset=juegosUltimaRonda)
            
        return render(request, 'torneos/detalle.html',
                      {'torneo': torneo, 'no_jugadores': no_jugadores, 'rondas': rondas, 'formSet': formSet,
                       'juegosUltimaRonda': juegosUltimaRonda, 'rondasAnteriores': rondasAnteriores, 'juegos': juegos,
                       'jugadores': jugadores, 'ultimaRonda': ultimaRonda})

--edit--
I have tried other approaches to forms.py

class ganadorForm(ModelForm):

    class Meta:
        model = Juego
        fields = ['ganador', 'jugadorA', 'jugadorB', 'torneo', 'ronda']

    def __init__(self, *args, **kwargs):
        super(ganadorForm, self).__init__(*args, **kwargs)
        self.fields['ganador'].queryset = Jugador.objects.filter(Q(id=self.instance.jugadorA.id) | Q(id=self.instance.jugadorB.id))

It throws 'NoneType' object has no attribute 'id'. So the question now is, how do I get the 'id' of jugadorA and jugadorB?

Carlos GM
  • 3
  • 6
  • In case the question is not understandable or does not make sense to you, I would also like to know so that we can improve it. Thanks :) – Carlos GM Dec 21 '21 at 19:30
  • Do you mind posting the view which shows this form? I think it's possible to apply a solution like this one: https://stackoverflow.com/a/1244586/530160 But it would be easier to say that if we could see the view too. – Nick ODell Dec 21 '21 at 20:15

0 Answers0