binpy
binpy

Reputation: 4194

Django: how does allow_tags and short_description works?

Basic way to do it in the models.py, an example:

class Foo(models.Model):
    title = models.CharField(max_length=200)
    ...

    def custom_tag(self):
        return ('custom: %s' % self.title)
    custom_tag.allow_tags = True
    custom_tag.short_description = _("Custom Tag")

or, if inside file of admin.py;

class FooAdmin(admin.ModelAdmin):
    list_display = ['title', 'custom_tag', ...]
    ...

    def custom_tag(self, instance):
        return ('custom: %s' % instance.title)
    custom_tag.allow_tags = True
    custom_tag.short_description = _("Custom Tag")


admin.site.register(Foo, FooAdmin)

My question is, how does allow_tags and short_description works? and where I can find the relevant documentation?

I can't found it at the documentation or also at source code

Upvotes: 1

Views: 3048

Answers (1)

Anonymous
Anonymous

Reputation: 12090

You're looking at the development version of the documentation. If you look at the one for Django 1.10 (scroll down to "Deprecated since version 1.9") you'll see that they're removing the allow_tags option and replacing it with other ways of achieving the same thing. There are plenty of examples on how to use short_description though since it's not deprecated.

If you really want to see the source code, here's the line where it gets the short description. Don't worry about the allow_tags since it's removed in 1.11 - it should now done automatically by marking string as safe using mark_safe().


As a side note, you don't need to add the custom_tag() method in both places. The admin looks in both the model and the admin class for it so one is sufficient. If it's not going to be used outside the admin, I would recommend placing it inside the admin class and avoid making the model more complex.

Upvotes: 7

Related Questions