Python 如何在Django中使用表单筛选结果(最佳方法)
我对python/django有点陌生,我想知道:在视图中使用表单过滤django结果的最佳方法是什么 我需要根据CandidateLook模型和CandidateToJob模型按头发颜色筛选候选人 但我得到一个错误:在我的forms.py行中语法无效: 如果自清理_数据['status']: 如果删除这一行,仍然会在中获得forms.py中的无效语法: 返回工作组候选人 提前谢谢 这是我的密码:Python 如何在Django中使用表单筛选结果(最佳方法),python,django,forms,filter,Python,Django,Forms,Filter,我对python/django有点陌生,我想知道:在视图中使用表单过滤django结果的最佳方法是什么 我需要根据CandidateLook模型和CandidateToJob模型按头发颜色筛选候选人 但我得到一个错误:在我的forms.py行中语法无效: 如果自清理_数据['status']: 如果删除这一行,仍然会在中获得forms.py中的无效语法: 返回工作组候选人 提前谢谢 这是我的密码: #models.py class Job(models.Model): candidate
#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)
#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)
以下是我的看法:
#views.py
class Screening(generic.DetailView):
model = Job
template_name = 'dashboard/screening.html'
def get_context_data(self, **kwargs):
context = super(Screening, self).get_context_data(**kwargs)
# Fetch the sender_id for each unread message.
# Count the number of times that the sender_id was included.
# Then convert the list of tuples into a dictionary for quick lookup.
sent_messages = dict(
self.request.user.received_messages.filter(
read_at__isnull=True
).values('sender_id').annotate(
messages_sent=Count('sender_id')
).values_list('sender_id', 'messages_sent')
)
form_cand = ScreeningForm(self.request.GET)
if form_cand.is_valid():
job_candidates = form_cand.filter_job_candidates(self.object)
else:
job_candidates = self.object.applied_to.all().order_by('candidate')
candidates = []
for candidate in self.object.applied_to.all().order_by('candidate'):
candidate.messages_sent = sent_messages.get(candidate.candidate.user.id)
candidates.append(candidate)
context['candidate_list'] = candidates
context['form_cand'] = form_cand
return context
#views.py
class Screening(generic.DetailView):
model = Job
template_name = 'dashboard/screening.html'
def get_context_data(self, **kwargs):
context = super(Screening, self).get_context_data(**kwargs)
# Fetch the sender_id for each unread message.
# Count the number of times that the sender_id was included.
# Then convert the list of tuples into a dictionary for quick lookup.
sent_messages = dict(
self.request.user.received_messages.filter(
read_at__isnull=True
).values('sender_id').annotate(
messages_sent=Count('sender_id')
).values_list('sender_id', 'messages_sent')
)
form_cand = ScreeningForm(self.request.GET)
if form_cand.is_valid():
job_candidates = form_cand.filter_job_candidates(self.object)
else:
job_candidates = self.object.applied_to.all().order_by('candidate')
candidates = []
for candidate in job_candidates:
candidate.messages_sent = sent_messages.get(candidate.candidate.user.id)
candidates.append(candidate)
context['candidate_list'] = candidates
context['form_cand'] = form_cand
return context
表格
#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
#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 }}
{% for candidate in candidate_list %}
{{ candidate }}
{% for status in candidate.status %}
{{ candidate.get_status_display }}
{% endfor %}
{% endfor %}
#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 %}
在朋友的帮助下解决了 如果您需要django表单来过滤结果,我就是这样做的 以下是我的看法:
#views.py
class Screening(generic.DetailView):
model = Job
template_name = 'dashboard/screening.html'
def get_context_data(self, **kwargs):
context = super(Screening, self).get_context_data(**kwargs)
# Fetch the sender_id for each unread message.
# Count the number of times that the sender_id was included.
# Then convert the list of tuples into a dictionary for quick lookup.
sent_messages = dict(
self.request.user.received_messages.filter(
read_at__isnull=True
).values('sender_id').annotate(
messages_sent=Count('sender_id')
).values_list('sender_id', 'messages_sent')
)
form_cand = ScreeningForm(self.request.GET)
if form_cand.is_valid():
job_candidates = form_cand.filter_job_candidates(self.object)
else:
job_candidates = self.object.applied_to.all().order_by('candidate')
candidates = []
for candidate in self.object.applied_to.all().order_by('candidate'):
candidate.messages_sent = sent_messages.get(candidate.candidate.user.id)
candidates.append(candidate)
context['candidate_list'] = candidates
context['form_cand'] = form_cand
return context
#views.py
class Screening(generic.DetailView):
model = Job
template_name = 'dashboard/screening.html'
def get_context_data(self, **kwargs):
context = super(Screening, self).get_context_data(**kwargs)
# Fetch the sender_id for each unread message.
# Count the number of times that the sender_id was included.
# Then convert the list of tuples into a dictionary for quick lookup.
sent_messages = dict(
self.request.user.received_messages.filter(
read_at__isnull=True
).values('sender_id').annotate(
messages_sent=Count('sender_id')
).values_list('sender_id', 'messages_sent')
)
form_cand = ScreeningForm(self.request.GET)
if form_cand.is_valid():
job_candidates = form_cand.filter_job_candidates(self.object)
else:
job_candidates = self.object.applied_to.all().order_by('candidate')
candidates = []
for candidate in job_candidates:
candidate.messages_sent = sent_messages.get(candidate.candidate.user.id)
candidates.append(candidate)
context['candidate_list'] = candidates
context['form_cand'] = form_cand
return context
表格
#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
#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 }}
{% for candidate in candidate_list %}
{{ candidate }}
{% for status in candidate.status %}
{{ candidate.get_status_display }}
{% endfor %}
{% endfor %}
#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 %}
#html
{{job.title}
{{form_cand.as_p}
{候选人列表%中的候选人的百分比}
{{候选人}}
{状态为候选人中的%status%}
{{candidate.get_status_display}
{%endfor%}
{%endfor%}
看一看django过滤器嗨,安迪,我试过django过滤器,也试过django数据过滤器,但不幸的是,我没能实现我所需要的。我刚刚更新了我的帖子,因为我忘了显示我的表格。