Sort list with Python + Django

1

Good afternoon guys, I'm having a problem, I've registered for the django admin a video that should be sent to my template, but I need these videos to be sorted by the position field I have in my models.py in the bank

models.py :

from django.db import models

class Video(models.Model):
    url = models.CharField(max_length=500, verbose_name='Link Embed')
    title = models.CharField(max_length=255, verbose_name='Título')
    description = models.TextField(max_length=1000, verbose_name='Descrição do video')
    start_date = models.DateField(verbose_name='Estará disponível quando')
    position = models.CharField(max_length=255 ,verbose_name='Qual posição')

    def __str__(self):
        return f'{self.title} {self.description} {self.start_date} - {self.url}'

video-list.html :

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div>
        {% for video in videos %}
        <div>
            <h1>
                {{video.title}}
            </h1>
            <iframe width="560" height="315" src="{{video.url}}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
            <p>
                {{video.description}}
            </p>
        </div>
        {% endfor %}
    </div>
</body>
</html>

views.py :

from django.shortcuts import render, redirect
from datetime import date
from .models import *
from .forms import *
import requests
import random

def videos_list(request):
    
    if 'lead_id' in request.session:        
        videos = Video.objects.all()
        for video in videos:
            print(video.titlle)
        return render(request, 'videos-list.html', {'videos': videos})

    return redirect('registrations:create_lead')
    
asked by anonymous 27.09.2018 / 16:30

1 answer

3

Just put the ordering field inside the model metadata - something like:

class Video(models.Model):
    url = ...
    ...
    position = ...
    ...
    class Meta:
        ordering = ["position"]

Django makes use of the Python character that allows you to declare a class within a class to allow you to place a number of options and settings within each template. It is important to note that this Meta is not used as a class - nor should it be called a metaclass, although historically, in Django, it has started like this - it is simply used as a namespace to declare options without the risk of conflicts with fields in your model.

Django's ORM uses "ordering" to organize your queries by default. (Note that your "position" field is text, which means "11" comes in front of "2", for example).

The documentation for ordering and other Meta options is here:

link

Another way is to explicitly specify the order_by in the query - change the line:

    videos = Video.objects.all()

By

    videos = Video.objects.order_by("position").all()

Documentation for query: link

    
27.09.2018 / 16:53