如何在django admin中显示布尔属性

时间:2022-03-12 20:03:17

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

众所周知,通过设置boolean属性,可以轻松地在Django管理员中显示方法返回值为boolean:

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

4 个解决方案

#1


18  

Waiting for better solutions to come up, I've solved it in the following way:

等待更好的解决方案出现,我已经通过以下方式解决了这个问题:

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

I'll then reference the _is_something method in the ModelAdmin subclass:

然后,我将引用ModelAdmin子类中的_is_something方法:

class MyModelAdmin(admin.ModelAdmin):
    list_display = ['_is_something']

And the is_something property otherwise:

而is_something属性否则:

if my_model_instance.is_something:
    print("I'm something")

#2


15  

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

这是我发现的最简单的方法,直接在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']

#3


2  

You need to create a shadowing function to the property in the model. What I mean is that you will need to recreate a function in the ModelAdmin class with the same name as the property defined in the main Model.

您需要为模型中的属性创建阴影函数。我的意思是你需要在ModelAdmin类中重新创建一个与主模型中定义的属性相同的函数。

Example:

例:

# Model
class Product(models.Model):

    @property
    def in_stock(self):
        # boolean check return
        return self.quantity > 0

...

...

# Django-modeladmin
class ProductAdmin(admin.ModelAdmin):
    list_display = ('in_stock', ...)
    def in_stock(self, instance):
        return instance.in_stock

    in_stock.boolean = True        

#4


-1  

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

如果将is_something定义为属性,它将是一个不可变对象,而不是一个函数,但该对象包含对fget属性中的装饰getter的引用。我认为Django管理界面使用该属性的getter,因此这可能有效

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

#1


18  

Waiting for better solutions to come up, I've solved it in the following way:

等待更好的解决方案出现,我已经通过以下方式解决了这个问题:

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

I'll then reference the _is_something method in the ModelAdmin subclass:

然后,我将引用ModelAdmin子类中的_is_something方法:

class MyModelAdmin(admin.ModelAdmin):
    list_display = ['_is_something']

And the is_something property otherwise:

而is_something属性否则:

if my_model_instance.is_something:
    print("I'm something")

#2


15  

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

这是我发现的最简单的方法,直接在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']

#3


2  

You need to create a shadowing function to the property in the model. What I mean is that you will need to recreate a function in the ModelAdmin class with the same name as the property defined in the main Model.

您需要为模型中的属性创建阴影函数。我的意思是你需要在ModelAdmin类中重新创建一个与主模型中定义的属性相同的函数。

Example:

例:

# Model
class Product(models.Model):

    @property
    def in_stock(self):
        # boolean check return
        return self.quantity > 0

...

...

# Django-modeladmin
class ProductAdmin(admin.ModelAdmin):
    list_display = ('in_stock', ...)
    def in_stock(self, instance):
        return instance.in_stock

    in_stock.boolean = True        

#4


-1  

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

如果将is_something定义为属性,它将是一个不可变对象,而不是一个函数,但该对象包含对fget属性中的装饰getter的引用。我认为Django管理界面使用该属性的getter,因此这可能有效

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