0

在 Django 中,我需要过滤数据并显示结果。例如

Alabama(20)
Iowa(12)

这里“阿拉巴马州,爱荷华州”是州名,括号内的“20,12”是否。特定州提供的工作机会。

模型.py

class User(models.Model):
     first_name= forms.CharField(max_length=30,widget=forms.TextInput())
     last_name = forms.CharField(max_length=30,widget=forms.TextInput())
     username = forms.CharField(max_length=30,widget=forms.TextInput())
     email = forms.EmailField(widget=forms.TextInput())
     password = forms.CharField(widget=forms.PasswordInput())
     companyname = forms.CharField(max_length=30,widget=forms.TextInput())

class jobs(models.Model):
     emp = models.ForeignKey(User, unique=False)
     title = models.CharField(max_length=30)
     referencecode = models.CharField(max_length=30)
     jobsummary = models.TextField()
     jobdetails = models.TextField()
     key_skills = models.CharField(max_length=30)
     states = models.CharField(max_length=30)

我试图给views.py就像

def search_result(request):

     details = jobs.objects.annotate().order_by('state')
     return render_to_response('searchresult.html', {'details': details})

templates
<ul>
{% for d1 in details %}
<li>{{ d1.state }}({{ d1.count }})</li>
{% endfor %}
</ul>

它只显示州名而不是计数。澄清一下。

4

2 回答 2

1

你可以这样做:

from django.db.models import Count
jobs.objects.values('states').annotate(count=Count('states'))
于 2013-04-23T12:57:35.147 回答
0

这似乎很棘手,但你可以通过参考“ https://docs.djangoproject.com/en/dev/ref/models/querysets/ ”来做到这一点

于 2013-04-23T12:36:38.047 回答