Django just sorts ListView by date

I am dealing with some kind of Django code that I have inherited from a developer, and you need to make a very simple change. The code has a list of tasks displayed through the Django ListView. My problem is very simple. When I go to the page, I see that tasks are sorted by date with an early first. I want to sort tasks in reverse order. I do not need filtering, passing parameters to URLs, etc. Here are the relevant parts of the files:

#models.py
from django.db import models
class Job(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    position =  models.ManyToManyField(Position)
    title = models.CharField(max_length=100)

#views.py
from .models import Job
class JobListView(ListView):
    template_name="jobs/list.html"
    model = Job
    paginate_by = 10

#list.html
{% for job in object_list %}
    <li class="display-list-item">
        <h4><strong><a href="{% url 'job_detail' pk=job.pk %}">{{job.title}}</a></strong></h4>
        <ul class="list-inline job-info-list">                                                    
            <span>{{job.created | timesince}} ago</span>
        </ul>               
    </li>
{% endfor %}


#urls.py
urlpatterns = [
url('^$', views.JobListView.as_view(), name='job_list')
]

As already mentioned, this causes the tasks to be displayed sorted by the "created" field. First created earlier. What is the fastest way to make first created first?

+4
1

models.py

from django.db import models
class Job(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    position =  models.ManyToManyField(Position)
    title = models.CharField(max_length=100)

    class Meta:
       ordering = ['-created']

views.py

from .models import Job
class JobListView(ListView):
    template_name="jobs/list.html"
    queryset = Job.objects.order_by('-created')
    paginate_by = 10
+4

Source: https://habr.com/ru/post/1659965/


All Articles