Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-235b-a22b-no-thinking· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

I wrote this admin class to change the way objects instances are being displayed on my…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwq-32b’s answer.
  1. User

    I wrote this admin class to change the way objects instances are being displayed on my django admin dashboard.

    Code: class ProfileAdmin(admin.ModelAdmin): model = Profile list_display = ('user', 'bio', 'location', 'birth_date')

    how do I set character limit on the bio field.

  2. Qwen Assistant

    In Django Admin, the list_display fields are rendered as-is by default, so if you want to limit the number of characters shown for a field like bio, you need to define a custom method in your ModelAdmin class and use that method in list_display.

    Here's how you can do it:

    ✅ Updated Code with Character Limit on bio:

    from django.utils.html import format_html
    from django.utils.text import Truncator
    
    class ProfileAdmin(admin.ModelAdmin):
        model = Profile
        list_display = ('user', 'limited_bio', 'location', 'birth_date')  # replaced 'bio' with 'limited_bio'
    
        def limited_bio(self, obj):
            return Truncator(obj.bio).chars(100)  # limits to 100 characters, adds '...' if truncated
        limited_bio.short_description = 'Bio'  # sets column header name
    

    🔍 Explanation:

    • Truncator from django.utils.text is a utility that safely truncates text.
    • .chars(100) ensures that up to 100 characters are displayed, and it appends an ellipsis (...) if the text was truncated.
    • You can also use .words(20) instead of .chars(100) if you'd prefer truncation based on word count.

    🧪 Optional: Preserve HTML in Bio (if needed)

    If bio contains HTML (e.g., from a rich text editor), and you want to preserve it without escaping:

    def limited_bio(self, obj):
        return format_html(Truncator(obj.bio).chars(100))
    

    ⚠️ Be cautious when using format_html — make sure the content is safe or sanitized to avoid XSS vulnerabilities.


    Let me know if you also want to apply this kind of truncation in the actual model detail view (i.e., the object change form)!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt