Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a boolean property in the django admin

Tags:

django

As we all know, displaying a method return value as boolean in the Django admin is easily done by setting the boolean attribute:

class MyModel(models.Model):     def is_something(self):         if self.something == 'something':             return True         return False     is_something.boolean = True 

How can you achieve the same effect for a property, like in the following case?

class MyModel(models.Model):     @property     def is_something(self):         if self.something == 'something':             return True         return False 
like image 320
GaretJax Avatar asked Oct 11 '12 14:10

GaretJax


1 Answers

this is the simplest way I found, directly in the ModelAdmin:

class MyModelAdmin(admin.ModelAdmin):     def is_something(self, instance):         return instance.something == "something"     is_something.boolean = True     is_something.short_description = u"Is something"      list_display = ['is_something'] 
like image 138
user7297468 Avatar answered Sep 20 '22 09:09

user7297468