class ProductView(ListView):
model = Products
queryset = Products.objects.filter(draft = False)
template_name = "products/index.html"
max_id = Products.objects.filter(draft=False).aggregate(max_id=Max("id"))['max_id']
random_products = []
if Products.objects.filter(draft=False).count() < 4:
raise Exception('Мало объектов для выборки')
while len(random_products) < 4:
pk = random.randint(1, max_id)
if pk in random_products:
continue
product = Products.objects.filter(draft=False, pk=pk).first()
if product:
random_products.append(product)