Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading