17

How can I set a verbose_name for a model's method, so that it might be displayed in the admin's change_view form?

example:

class Article(models.Model):
    title = models.CharField(max_length=64)
    created_date = models.DateTimeField(....)
    def created_weekday(self):
        return self.created_date.strftime("%A")

in admin.py:

class ArticleAdmin(admin.ModelAdmin):
    readonly_fields = ('created_weekday',)
    fields = ('title', 'created_weekday')

Now the label for created_weekday is "Created Weekday", but I'd like it to have a different label which should be i18nable using ugettext_lazy as well.

I've tried

    created_weekday.verbose_name=...

after the method, but that did not show any result. Is there a decorator or something I can use, so I could make my own "verbose_name" / "label" / whateverthename is?

1 Answer 1

23

list_display

created_weekday.short_description = 'Foo'

This solution requires the method to be defined in the ModelAdmin class. You can call a Model method (eg: get_created_weekday) from ModelAdmin like:

def created_weekday(self, obj):
    return obj.get_created_weekday()
2
  • 1
    thanks, that works. no wonder I didn't find it in the documentation - it is written under "ModelAdmin.list_display"...
    – mawimawi
    May 23, 2010 at 19:06
  • 3
    Yes, but the documentation for readonly_fields does say "This option behaves nearly identical to ModelAdmin.list_display". May 23, 2010 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.