- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我所有的 django 模型都有 unicode 函数,目前这些往往是这样写的:
def __unicode__(self):
return u'Unit: %s -- %s * %f' % (self.name, self.base.name, self.mul)
但是,像 Pythonista 一样编码,位于 http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#string-formatting指出 self.__dict__
是一个字典,因此上面的内容可以简化为:
def __unicode__(self):
return u'Unit: %(name)s -- %(base.name)s * %(mul)f' % self.__dict__
这有效,除了“base.name”,因为 python 试图查找 self.__dict__['base.name']
但失败了,而 self.base.name 有效。
是否有一种优雅的方式来完成这项工作,即使您需要遵循外键关系?
最佳答案
%
字符串格式不支持属性访问,但是 format
(自 2.6 起):
def __unicode__(self):
return u'Unit: {name:s} -- {base.name:s} * {mul:f}'.format(**self.__dict__)
关于python - Django:更多 pythonic __unicode__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13305379/
我在 python 中有以下类 class myTest: def __init__(self, str): self.str = str def __unicode_
有没有办法包含模型的 __unicode__ 方法 搜索字段 元组?我不想创建特殊字段来存储 的结果__unicode__ 在模型中,因为这个值取决于其他几个模型...... 最佳答案 不,这是不可能
我正在处理一些 django 代码,其中所有模型都有 __unicode__ 方法,如下所示: def __unicode__(self): return str(self.id) + ";
我有一个如下所示的模型类: class Address(models.Model): # taking length of address/city fields from existing
我有两个模型, class Person(models.Model): first_name = models.CharField(max_length=50) last_name =
假设我有一个名为 Animal 的类和一个名为 Dog 的子类。如何从 Dog 类访问 Animal 的 unicode 定义? class Animal: def __unicode_
在 Django 管理中删除具有多对多关系的对象时,尽管两个连接表具有 __unicode__ 方法,但我没有显示名称。 即。它只是显示... (用户-公司关系:User_company 对象) 我想
我正在使用 Django 运行一个小的患者列表。在管理界面中,我有一些在 admin.py 文件中定义的列。 class InpatientAdmin(admin.ModelAdmin): list_
如何让 Django 模型 __unicode__ 方法返回多个值到多个列? 最佳答案 只需将以下内容添加到类中: def __unicode__(self): return '%s %s'
(Django 1.x,Python 2.6.x) 我有以下模型: class Animal(models.Model): pass class Cat(Animal): def __unic
首先,有没有? 如果没有,是否有一种很好的方法来强制执行类似的操作 print '%s' % obj 要调用 obj.__unicode__ 而不是 obj.__str__? 最佳答案 只需使用 un
对于何时应该实现 __str__() 与 __unicode__() 是否有 python 约定。我见过类覆盖 __unicode__() 比 __str__() 更频繁,但它似乎并不一致。是否有具体
在安装了 Django 的开发源代码以及来自源代码的 PostgreSQL 以及来自源代码的所有其他需要的东西后,我正在学习 Django 教程。我正在尝试在 Ubuntu 12.10 上使用 pyt
Class NickName(models.Model): name = models.CharField() def __unicode__(self): retur
我所有的 django 模型都有 unicode 函数,目前这些往往是这样写的: def __unicode__(self): return u'Unit: %s -- %s * %f' %
我正在尝试通过自己开发一个网站来打破僵局,我从创建一些注册表页面并列出数据库记录开始。 我对 __unicode__ 的事实感到困惑方法不打印我的记录的用户名和 __str__做! 我知道使用 __u
我想在 Django 管理界面中显示模型的用户名,但不太确定该怎么做。 The models.py: class Adult(models.Model): user =
在 django 管理员中,我有一个 TabularInline 用于 ManyToMany 字段,并设置了 raw_id_fields 。它在 html 输入字段旁边显示对象的 unicode()。
我正在从 djangobook 学习 Django,在第 6 章中,安装管理应用程序的地方,syncdb 命令在 models.py 中返回语法错误(返回 u'%s %s' % (self.first
如果我写类似的东西 class Chip(models.Model): name = models.CharField(max_length=16) shortname = mode
我是一名优秀的程序员,十分优秀!