Django ver 1.10
如何有条件地检查变量是否为空并显示“ - ”?
即如果未设置get_followers
和/或get_following
,则显示“ - ”
@admin.register(Application)
class ApplicationAdmin(admin.ModelAdmin):
list_display = [
'name',
'get_following',
'get_followers',
'get_friends',
'manager',
]
答案 0 :(得分:1)
以下是list_display的文档:
Amazon S3 client putObject()
function
您可以使用此方法。例如:
class Application(models.Model):
name = models.CharField(max_length=100)
following = ...
followers = ...
friends = ...
def get_following(self):
if not self.following:
return "-"
return self.following
在您的admin.py中,您只需输入方法的名称
即可@admin.register(Application)
class ApplicationAdmin(admin.ModelAdmin):
list_display = [
'name',
'get_following',
...
]