如何使用Django中的表单过滤结果(最佳方法)

2024-04-27 04:05:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python/django的新手,我想知道:在django中使用视图中的表单过滤结果的最佳方法是什么?在

我需要根据发色、CandidateLook模型和根据CandidateToJob模型按状态筛选候选人。在

但是我得到一个错误:在我的表单.py排队: 如果自行清理的数据[状态]:

如果我删除这行,仍然得到无效的语法表单.py在: 返回工作候选人

提前谢谢。在

这是我的代码:

#models.py
class Job(models.Model):
    candidate = models.ManyToManyField('Candidate', through='CandidateToJob')
    title = models.CharField(max_length=500)
    ...

class Candidate(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    photo = models.ImageField(upload_to=user_profile_path)

class CandidateLook(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    HAIRCOLOR_CHOICES = (
        ('1', 'Black'),
        ('2', 'Brown'),
        ('3', 'Blonde'),
        ('4', 'Red')
    )
    haircolor = models.CharField(max_length=2, choices=HAIRCOLOR_CHOICES)

class CandidateToJob(models.Model):
    job = models.ForeignKey(Job, related_name='applied_to')
    candidate = models.ForeignKey(Candidate, related_name='from_user')
    STATUS_CHOICES = (
        ('0', 'New'),
        ('1', 'Not approved'),
        ('2', 'Approved')
     )
    status = models.CharField(max_length=2, choices=STATUS_CHOICES)

以下是我的观点:

^{pr2}$

表格

#forms.py
class StatusForm(ModelForm):
    STATUS_CHOICES = (
        ('0', 'New'),
        ('1', 'Not approved'),
        ('2', 'Approved')
    )
    status = forms.ChoiceField(required=False, widget=forms.RadioSelect(attrs={'class': ''}), choices=STATUS_CHOICES)

    class Meta:
        model = CandidateToJob
        exclude = ['candidate', 'job']

class HairColorForm(ModelForm):
    HAIRCOLOR_CHOICES = (
        ('1', 'Black'),
        ('2', 'Brown'),
        ('3', 'Blonde'),
        ('4', 'Red')
    )
    status = forms.ChoiceField(required=False, widget=forms.RadioSelect(attrs={'class': ''}), choices=HAIRCOLOR_CHOICES)

    class Meta:
        model = CandidateLook

class ScreeningForm(forms.Form):
    haircolor = forms.ChoiceField(choices=CandidateLook.HAIRCOLOR_CHOICES, required=False)
    status = forms.ChoiceField(choices=CandidateToJob.STATUS_CHOICES, required=False)

    def filter_job_candidates(job):
        assert self.is_valid()

        job_candidates = job.applied_to.all().order_by('candidate')

        if self.cleaned_data['haircolor']:
            job_candidates = job_candidates.filter(candidate__candidatelook__haircolor=self.cleaned_data['haircolor']       

        if self.cleaned_data['status']:
            job_candidates = job_candidates.filter(status=self.cleaned_data['status']

        return job_candidates

以及模板:

#html
{{ job.title }}

{% for candidate in candidate_list %}

    {{ candidate }}

    {% for status in candidate.status %}

         {{ candidate.get_status_display }}

    {% endfor %}

{% endfor %}

Tags: selfmodelsstatusjobformscandidateclasschoices
1条回答
网友
1楼 · 发布于 2024-04-27 04:05:29

在朋友的帮助下解决了。在

如果你需要django表单来过滤结果,我就是这样做的。在

#models.py
class Job(models.Model):
    candidate = models.ManyToManyField('Candidate', through='CandidateToJob')
    title = models.CharField(max_length=500)
    ...

class Candidate(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    photo = models.ImageField(upload_to=user_profile_path)

class CandidateLook(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    HAIRCOLOR_CHOICES = (
        ('1', 'Black'),
        ('2', 'Brown'),
        ('3', 'Blonde'),
        ('4', 'Red')
    )
    haircolor = models.CharField(max_length=2, choices=HAIRCOLOR_CHOICES)

class CandidateToJob(models.Model):
    job = models.ForeignKey(Job, related_name='applied_to')
    candidate = models.ForeignKey(Candidate, related_name='from_user')
    STATUS_CHOICES = (
        ('0', 'New'),
        ('1', 'Not approved'),
        ('2', 'Approved')
     )
    status = models.CharField(max_length=2, choices=STATUS_CHOICES)

以下是我的观点:

^{pr2}$

表格

#forms.py
class StatusForm(ModelForm):
    STATUS_CHOICES = (
        ('0', 'New'),
        ('1', 'Not approved'),
        ('2', 'Approved')
    )
    status = forms.ChoiceField(required=False, widget=forms.RadioSelect(attrs={'class': ''}), choices=STATUS_CHOICES)

    class Meta:
        model = CandidateToJob
        exclude = ['candidate', 'job']

class HairColorForm(ModelForm):
    HAIRCOLOR_CHOICES = (
        ('1', 'Black'),
        ('2', 'Brown'),
        ('3', 'Blonde'),
        ('4', 'Red')
    )
    status = forms.ChoiceField(required=False, widget=forms.RadioSelect(attrs={'class': ''}), choices=HAIRCOLOR_CHOICES)

    class Meta:
        model = CandidateLook

class ScreeningForm(forms.Form):
    haircolor = forms.ChoiceField(choices=CandidateLook.HAIRCOLOR_CHOICES, required=False)
    status = forms.ChoiceField(choices=CandidateToJob.STATUS_CHOICES, required=False)

    def filter_job_candidates(self, job):
        assert self.is_valid()

        job_candidates = job.applied_to.all().order_by('candidate')

        if self.cleaned_data['haircolor']:
            job_candidates = job_candidates.filter(candidate__candidatelook__haircolor=self.cleaned_data['haircolor'])      

        if self.cleaned_data['status']:
            job_candidates = job_candidates.filter(status=self.cleaned_data['status'])

        return job_candidates

以及模板:

#html
{{ job.title }}

<form method="get" action=".">
    {{ form_cand.as_p }}
    <input type="submit" value="Filtrar" />
</form>

{% for candidate in candidate_list %}

    {{ candidate }}

    {% for status in candidate.status %}

         {{ candidate.get_status_display }}

    {% endfor %}

{% endfor %}

相关问题 更多 >