我希望我的模型有两个字符串表示形式:一个显示在后端日志中用于调试目的,另一个用于在HTML中表示模型时显示给最终用户.现在,我只是压倒一切__unicode__().有没有办法做到这一点?
使用属性
class SomeThing( models.Model ):
foo=
bar=
baz=
def __unicode__( self ):
return "{0} {1}".format( self.foo, self.bar )
@property
def details( self ):
return repr( dict( foo=self.foo, bar=self.bar, baz=self.baz ) )
Run Code Online (Sandbox Code Playgroud)
现在你可以登录了 someObject.details
| 归档时间: |
|
| 查看次数: |
13256 次 |
| 最近记录: |