How to display a boolean property in the django admin

后端 未结 5 1375
说谎
说谎 2021-01-31 15:47

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

class MyMo         


        
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 16:37

    If you define is_something as a property, it will be an immutable object, instead of a function, but that object contains a reference to the decorated getter in the fget attribute. I think that the Django admin interface use the getter of that property, thus this may works

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

提交回复
热议问题