Py学习  »  Django

Django外键反向过滤

K. Jones • 6 年前 • 2944 次点击  

我对Django还比较陌生,仍然在努力进行一些外键过滤,我很感谢你对我的问题的帮助。我在下面有两个模型,在我的职位更新表中,我需要“应聘者”字段选择仅作为该职位的申请者。

class Position(models.Model):
    title = models.CharField(max_length=128)
    candidate = models.ForeignKey('careers.Applicant',
        on_delete=models.SET_NULL,
        related_name='candidates',
        blank=True,
        null=True
    )

class Applicant(models.Model):
    first_name = models.CharField(max_length=128)
        blank=False,
    )
    position = models.ManyToManyField(Position,
        related_name='applicants',
        blank=True
    )

以我的形式,我尝试了以下每一项:

class PositionUpdateForm(forms.ModelForm):
    candidate = forms.ModelChoiceField(queryset=Applicant.objects.filter(???))

def __init__(self, *args, **kwargs):
    super(PositionUpdateForm, self).__init__(*args, **kwargs)
    self.fields['candidate'].queryset = Applicant.objects.filter(???)

谢谢你的帮助。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52785
文章 [ 1 ]  |  最新文章 6 年前