我想在模板中使用计数或长度功能,但我不知道如何去做。我想单独计算所有类别的项目
models.py
class Product(PolymorphicModel):
title = models.CharField(max_length=100, blank=True)
image = models.ImageField(upload_to='product', default=None)
quantity = models.IntegerField(null=False)
is_available = models.BooleanField(default=True, null=False)
price = models.IntegerField(null=False, blank=False, default=15)
popularity = models.IntegerField(default=0)
def __str__(self):
return str(self.title)
def get_absolute_url(self):
return reverse("ProductDetail", args=[str(self.pk)])
@property
def model_name(self):
return self._meta.model_name
class CD(Product):
GENRE_CHOICES = (
('Rock', 'Rock'),
('Pop', 'Pop'),
('Reggae', 'Reggae'),
('Disco', 'Disco'),
('Rap', 'Rap'),
('Electronic music', 'Electronic music'),
)
band = models.CharField(max_length=100, null=False, blank=False)
tracklist = models.TextField(max_length=500, null=False, blank=False)
genre = models.CharField(max_length=100, choices=GENRE_CHOICES, null=False, blank=False)
views.py 所有元素的简单列表
class ProductListView(View):
def get(self, request):
products = CD.objects.all()
return render(request, 'products/statistics.html', {'products': products})
1条答案
按热度按时间polhcujo1#
可以使用
count
方法统计查询集结果:文件:https://docs.djangoproject.com/en/4.1/ref/models/querysets/