开发者

Delimiting choices in ModelChoiceField

开发者 https://www.devze.com 2023-01-05 12:09 出处:网络
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.

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.

Models.py

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

for开发者_JAVA百科ms.py

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))

extract of views.py

[...]
form = DeudaForm(request.user)

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.

Final 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)


Try to put it in the form's __init__:

class DeudaForm(forms.ModelForm):

    class Meta:
        model = Deuda
        exclude = ('propietario',)

    def __init__(self, propietario):
        forms.ModelForm.__init__(self)
        self.fields['adeudado'].queryset = User.objects.exclude(pk=d.propietario.pk)
0

精彩评论

暂无评论...
验证码 换一张
取 消