Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
How to fill out a list from the database?

I need to bind to each alias its rating, which would then display in the template, and order products by decreasing. How can I do that?

class Items(models.Model):
    name = models.CharField(max_length=15, verbose_name='Название товара', default='')
    price = models.IntegerField(default=0, verbose_name='Цена')
    # image = models.CharField(max_length=255, verbose_name='Картинка', default='')
    image = models.ImageField(null=True, blank=True, upload_to='image/', verbose_name='Изображение')
    alias = models.SlugField(verbose_name='Alias товара', default='')
    rating = models.FloatField(default=0, verbose_name='Рейтинг')



def popular(request):
    title = 'Популярные'
    products = Items.objects.all()
    category = Category.objects.all()

    context = {
        'products': products,
        'category': category,
        'title': title,
    }
    return render(request, 'popular/main.html', context)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can fetch the result set from the database by ordering it the way you want:

products = Items.objects.order_by('-rating')

And in the template:

{% for product in products %}
    {{product.alias}} - {{product.rating}}
{% endfor %}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!