1

我正在使用 SQLObject,我有以下内容:

# update foo if it exists, otherwise create a new one
if self.foo_exists:
  Foo.get(foo_id).set(name = foo['name'], ip = foo['ip'], port = foo['port'], mode = foo['mode'], max_conn = foo['max_conn']) 
else:
  Foo(name = foo['name'], ip = foo['ip_address'], port = foo['port'], mode = foo['mode'], max_conn = foo['max_conn'])

它运作良好,但我真的很想删除重复。我将完全相同的论点传递给Foo().set()

我尝试传递一个 dict 但 SQLObject 不支持它。

4

1 回答 1

1
try:
    obj = Foo.get(foo_id)
    obj.set(**d)
except SQLObjectNotFound:
    Foo(**d)

它支持dict,使用**dict。它也是重复的,但不是那么响亮。

于 2015-01-30T14:46:23.943 回答