Note that there are some explanatory texts on larger screens.

plurals
  1. PODelimiting choices in ModelChoiceField
    text
    copied!<p>I'm new to python and to django so this question will probably be easy to solve but I can't get it to work. Basically I have a model which contains two foreign keys of User type. I'm building a form in which I want to remove one of the choices of a ModelChoiceField based on another field. I want the user to be unable to select himself in the form. My code.</p> <p>Models.py</p> <pre><code>from django.db import models from django.contrib.auth.models import User class Deuda(models.Model): ESTADOS = (("S", "Sin pagar"), ("P", "Pagada"), ("PC", "Pagada (Confirmada)")) propietario = models.ForeignKey(User, verbose_name="Propietario", related_name="propietario_deuda") adeudado = models.ForeignKey(User, verbose_name="Adeudado", related_name="adeudado_deuda") cantidad = models.FloatField("Cantidad") descripcion = models.TextField("Descripcion") estado = models.CharField(max_length=2, choices=ESTADOS) def __str__(self): return self.descripcion </code></pre> <p>forms.py</p> <pre><code>from django import forms from housemanager.iou.models import Deuda from django.contrib.auth.models import User class DeudaForm(forms.ModelForm): class Meta: model = Deuda exclude = ('propietario',) def __init__(self, propietario): d = Deuda() d.propietario = propietario forms.ModelForm.__init__(self, instance=d) adeudado = forms.ModelChoiceField(queryset=User.objects.exclude(pk=d.propietario.pk)) </code></pre> <p>extract of views.py</p> <pre><code>[...] form = DeudaForm(request.user) </code></pre> <p>So my idea is to pass the user to the form so it can define a ModelChoiceField that does not include it. My code doesn't work because d is not in the scope while setting 'adedado'. I'm sure there has to be many ways of achieving this goal feel free to change anything on my design.</p> <p>Final code:</p> <pre><code>from django import forms from housemanager.iou.models import Deuda from django.contrib.auth.models import User class DeudaForm(forms.ModelForm): class Meta: model = Deuda exclude = ('propietario',) def __init__(self, propietario): d = Deuda() d.propietario = propietario forms.ModelForm.__init__(self, instance=d) self.fields['adeudado'].queryset = User.objects.exclude(pk=propietario.pk) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload