class SelectListView(ListView):
model=MyModel
template_name = "/select_list.html"
context_object_name = 'selectlist'
queryset = MyModel.objects.all().values('ItemType_id')
paginate_by = 10
def get_context_data(self, **kwargs):
context = super(SelectListView, self).get_context_data(**kwargs)
context['range'] = range(context["paginator"].num_pages)
return context
models.py
class ItemType(models.Model):
ItemType=models.CharField(unique=True,max_length=40)
class MyModel(models.Model):
Type=models.ForeignKey(ItemType)
ItemName=models.CharField(unique=True,max_length=40)
答案 0 :(得分:0)
你必须做这样的事情:
from django.db.models import Count
MyModel.objects.values('Type').annotate(dcount=Count('Type'))
MyModel.objects.values('Type__id').annotate(dcount=Count('Type__id'))
您还可以阅读this document can help you.... 祝你好运!