How to modify objects in django ListView?

I have a ListView for a blog with fields of "title" and "text".
How to change "text" for example I want to summarize it something like text[:100]

I don’t want to change the database. I think the solution is modifying get_queryset but don’t know how to implement it.

>Solution :

You can modify the get_queryset method in your view to summarize the "text" field.

Here’s an example of how you can do it in a Django view using the get_queryset method:

from django.shortcuts import render
from .models import Blog

class BlogListView(ListView):
    model = Blog
    template_name = 'blog_list.html'

    def get_queryset(self):
        queryset = super().get_queryset()
        for blog in queryset:
            blog.text = blog.text[:100] + '...'
        return queryset

Leave a Reply