我在OpenERP/PostgreSQL中有一个表,其中包含以下列:name和description.
我为唯一名称添加了以下验证:
_sql_constraints = [('unique_name', 'unique(name)', 'A record with the same name already exists.')]
Run Code Online (Sandbox Code Playgroud)
它工作正常,但它区分大小写.目前,它接受诸如"Mickey","MICKEY"和"mickey"之类的值:
Wrong Way:
--------------------------
| name | description |
--------------------------
| mickey | not a mouse |
--------------------------
| MICKEY | not a mouse |
--------------------------
| Mickey | not a mouse |
--------------------------
Run Code Online (Sandbox Code Playgroud)
有没有办法修改验证代码,以便它不允许用户添加几个值,如"米奇","MICKEY"和"米奇"?如何使唯一密钥验证不敏感?
Right Way:
--------------------------------
| name | description |
--------------------------------
| mickey | not a mouse |
--------------------------------
| mickey mouse | is a mouse |
--------------------------------
| …Run Code Online (Sandbox Code Playgroud) 我有一个enum国籍:
class Nationality:
Poland='PL'
Germany='DE'
France='FR'
Run Code Online (Sandbox Code Playgroud)
如何以这种或类似的方式将此枚举转换为int:
position_of_enum = int(Nationality.Poland) # here I want to get 0
Run Code Online (Sandbox Code Playgroud)
我知道如果我有代码,我可以这样做:
counter=0
for member in dir(Nationality):
if getattr(Nationality, member) == code:
lookFor = member
counter += 1
return counter
Run Code Online (Sandbox Code Playgroud)
但我没有,这种方式对于python来说太大了.我确信有一些更简单的东西.
我正在使用Popen因为我需要env,就像这样:
Popen(
["boto-rsync", "..."],
env={"PATH":"/Library/Frameworks/Python.framework/Versions/2.7/bin/"},
)
Run Code Online (Sandbox Code Playgroud)
问题是Popen将命令作为新线程运行.有什么办法,我可以传递env到subprocess.call或阻止Popen从创建一个新的线程?感谢名单
如何使用python脚本将.csv文件转换为.dbf文件?我发现这一块的代码,在网上,但我不能肯定它是多么可靠.有没有具有此功能的模块?
如何使用knockout.js焦点设置由绑定到数组的模板创建的元素?
我有一个绑定到表的可观察数组,其中每一行都是一组输入元素,以允许编辑数组元素的属性.底部是一个"Add"按钮,它将一个新元素推入数组,创建一个新的输入字段行.
我要做的是"Add"在按下按钮后将焦点设置为新创建的输入字段中的第一个.
HTML:
<html>
<head>
<script src="http://cdn.jsdelivr.net/knockout/3.0.0/knockout.debug.js"></script>
</head>
<body>
<table data-bind='foreach: Attributes'>
<tr>
<td><input type='text' data-bind='value: Name, disable: HardCoded/></td>
<td><input type='text' data-bind='value: Description'/></td>
<td><button data-bind="click: $parent.removeAttribute">Delete</button></td>
</tr>
</table>
<button data-bind="click: addAttribute">Add attribute</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
function Attribute(id, name, description, hardcoded) {
var self=this;
self.AttributeID=ko.observable(id || 0);
self.Name=name || '';
self.Description=description || '';
self.HardCoded=hardcoded || false;
self.nameFocus = true;
}
function AttributeSchema(attributeArray) {
var self=this;
// Properties
self.Attributes=ko.observableArray(attributeArray);
// Operations
self.addAttribute=function() {
self.Attributes.push(new Attribute()); …Run Code Online (Sandbox Code Playgroud) 在我的OpenERP安装中,我有以下字段,之前不需要,但我将所需的参数更改为True.
'fiscal_position': fields.many2one(
'account.fiscal.position',
'Fiscal Position',
required=True,
readonly=True,
states={'draft':[('readonly',False)]}
),
Run Code Online (Sandbox Code Playgroud)
在调试日志中,我看到ORM尝试为数据库中的该字段设置非空约束.
2013-01-04 15:28:56 EET STATEMENT: ALTER TABLE "account_invoice"
ALTER COLUMN "fiscal_position" SET NOT NULL
Run Code Online (Sandbox Code Playgroud)
我怎么能防止这种情况?我的想法是获得所需的True标志,仅用于新记录并且没有NOT NULL约束.在其他情况下发生PostgreSQL完整性错误:
IntegrityError: null value in column "fiscal_position" violates
not-null constraint
Run Code Online (Sandbox Code Playgroud)
那么,我怎样才能在表单视图中有一个必填字段,而不使ORM触及数据库方案约束?或者如何根据对象的状态动态更改所需的字段?
我正在尝试从Windows XP Professional中安装Python 3; 但是我收到以下屏幕(没有安装按钮):
在我看来,这显然是一个未报告的错误或渲染问题.这个问题怎么缓解了?如果可以克服这个问题,它是否是其他相关问题的标志?
从python wheel分发包中排除文件的正确方法是什么?
编辑MANIFEST.in没有任何效果,我找不到有关此详细信息的信息.
注意 这个问题与Python 3 Enum数据类型无关,它只是我正在使用的示例.
使用PEP 3115, Python 3添加了1方法,以便在创建类时允许使用自定义命名空间.例如,新数据类型用于返回私有实例以用作新类的命名空间.__prepare__typeEnum__prepare___EnumDictEnum
但是,我在SO 2中看到了几个关于EnumMeta子类的例子,在元类__new__方法中为类创建了一个新的命名空间,而不是调用__prepare__方法来获取新的命名空间type(clsdict)().这样做有风险吗?
1 签名__prepare__:
@classmethod
def __prepare__(metacls, cls, bases, **kwds):
Run Code Online (Sandbox Code Playgroud)
并为__new__:
def __new__(metacls, cls, bases, clsdict, **kwds):
Run Code Online (Sandbox Code Playgroud)
2 示例使用type(clsdict):
从这个答案
class CountryCodeMeta(enum.EnumMeta):
def __new__(metacls, cls, bases, classdict):
data = classdict['data']
names = [(country['alpha-2'], int(country['country-code'])) for country in data]
--> temp = type(classdict)()
for name, value in …Run Code Online (Sandbox Code Playgroud) index = [0, 2, 5]
s = "I am like stackoverflow-python"
for i in index:
s = s[i].upper()
print(s)
IndexError: string index out of range
Run Code Online (Sandbox Code Playgroud)
据我所知,在第一次迭代中,字符串s变为第一个字符,在这种特殊情况下为大写"I".但是,我试图在没有"s ="的情况下使用swapchcase()它,而是使用它,但它没有用.
基本上,我正在尝试s使用Python 3.X将索引字母打印为大写的字符串
python ×9
python-3.x ×5
openerp ×2
call ×1
csv ×1
dbf ×1
enums ×1
knockout.js ×1
metaclass ×1
popen ×1
postgresql ×1
python-3.5 ×1
python-wheel ×1
string ×1
subprocess ×1
uppercase ×1
windows ×1
windows-xp ×1