You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django Admin列表页:将模型方法返回值改为显示Yes/No而非图标

Got it, let's fix this for you! When you set boolean=True on your model method, Django Admin automatically renders those boolean values as green/red icons. To show "Yes/No" text instead, you have a couple of straightforward options depending on your needs:

Option 1: Modify the Model Method to Return Text Directly

If you don't need the method to return a boolean value for other parts of your code, you can adjust it to return "Yes" or "No" strings, and set a clear display name for the Admin column:

from django.utils import timezone
import datetime

class Question(models.Model):
    pub_date = models.DateTimeField('date published')

    def was_published_recently(self):
        now = timezone.now()
        is_recent = now - datetime.timedelta(days=1) <= self.pub_date <= now
        return "Yes" if is_recent else "No"
    
    # Set the column name that shows up in Admin
    was_published_recently.short_description = 'Published Recently?'

Just remove the was_published_recently.boolean = True line — without this flag, Django Admin will render the returned string directly instead of using icons.

Option 2: Keep the Boolean Method, Add a Display Wrapper in Admin

If you still need the was_published_recently method to return a boolean (for other logic in your app), you can create a separate display method in your Admin class to handle the "Yes/No" conversion:

from django.contrib import admin
from .models import Question

class QuestionAdmin(admin.ModelAdmin):
    # Use our custom display method instead of the original boolean method
    list_display = ('pub_date', 'published_recently_display')

    def published_recently_display(self, obj):
        # Reuse the existing boolean method and convert to text
        return "Yes" if obj.was_published_recently() else "No"
    
    # Set the column name for Admin
    published_recently_display.short_description = 'Published Recently?'

admin.site.register(Question, QuestionAdmin)

Bonus: Add Colored Text for Better Visibility

If you want to keep the visual clarity of the original icons but use text, you can use format_html to add color styling:

from django.utils.html import format_html
from django.contrib import admin
from .models import Question

class QuestionAdmin(admin.ModelAdmin):
    list_display = ('pub_date', 'published_recently_display')

    def published_recently_display(self, obj):
        if obj.was_published_recently():
            return format_html('<span style="color: #2ecc71; font-weight: bold;">Yes</span>')
        else:
            return format_html('<span style="color: #e74c3c; font-weight: bold;">No</span>')
    
    published_recently_display.short_description = 'Published Recently?'

admin.site.register(Question, QuestionAdmin)

This gives you colored text that's easy to scan, just like the original icons but with your preferred text labels.

内容的提问来源于stack exchange,提问作者vasim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:44:06