Hi guys, I'm currently facing a strange thing :)
I make use of datatables (jquery datatables.net) and jeditable to
update values in my db. If the user clicks on a value, he changes it
and sends it, thus, only the value of this field.
On the server part, i then do a
db.alarms[request.vars.id].update_record(field=value).
Unfortunately, i have to have a unicity condition for this table, and
thus, with the previous statement, i get an error.
Here's some code to help understand..
db.define_table('alarms',
Field('triggerName', type = 'string', label = T('Trigger Name'),
required = True, notnull = True),
Field('triggerValue', type = 'integer', label = T('Trigger
Value'), required = True, notnull = True),
Field('message', type = 'string', label = T('Message'), required =
True, notnull = True),
Field('alarmScope', type = 'string', label = T('Alarm Scope'),
required = True, notnull = True),
Field('unicity', type = 'string', compute = lambda x:
x.triggerName + str(x.triggerValue), unique = True),
format = '%(triggerName)s - %(triggerValue)s',
migrate = settings.migrate)
db.alarms.triggerValue.requires =
IS_NOT_IN_DB(db(db.alarms.triggerName == request.vars.triggerName),
'alarms.triggerValue')
If i want to update one of the fields, I have absolutely to also
update (even if the don't change) triggerName and triggerValue (fields
for unicity condition), and thus I need some test dependent on which
field is updated..
Here's an exemple:
In [15]: db.alarms[2].update_record(message='rz')
---------------------------------------------------------------------------
KeyError Traceback (most recent call
last)
/Users/joseph/Documents/workspace/web2py/<ipython console> in
<module>()
/Users/joseph/Documents/workspace/web2py/gluon/dal.pyc in <lambda>(_,
**a)
1245 if field_type == 'id':
1246 id = colset[field.name]
-> 1247 colset.update_record = lambda _ = (colset,
table, id), **a: update_record(_, a)
1248 colset.delete_record = lambda t = table, i
= id: t._db(t._id==i).delete()
1249 for (referee_table, referee_name) in \
/Users/joseph/Documents/workspace/web2py/gluon/dal.pyc in
update_record(pack, a)
4433 c = dict([(k,v) for (k,v) in b.items() \
4434 if k in table.fields and not k=='id'])
-> 4435 table._db(table._id==id).update(**c)
4436 for (k, v) in c.items():
4437 colset[k] = v
/Users/joseph/Documents/workspace/web2py/gluon/dal.pyc in update(self,
**update_fields)
4392 def update(self, **update_fields):
4393 tablename = self.db._adapter.get_table(self.query)
-> 4394 fields =
self.db[tablename]._listify(update_fields,update=True)
4395 self.delete_uploaded_files(update_fields)
4396 return
self.db._adapter.update(tablename,self.query,fields)
/Users/joseph/Documents/workspace/web2py/gluon/dal.pyc in
_listify(self, fields, update)
3748 new_fields.append((ofield,ofield.update))
3749 elif ofield.compute:
-> 3750
new_fields.append((ofield,ofield.compute(Row(fields))))
3751 elif not update and ofield.required:
3752 raise SyntaxError,'Table: missing required
field: %s' % name
/Users/joseph/Documents/workspace/web2py/applications/hmiwebsite/
models/model_alarms.py in <lambda>(x)
7 Field('message', type = 'string', label = T('Message'),
required = True, notnull = True),
8 Field('alarmScope', type = 'string', label = T('Alarm
Scope'), required = True, notnull = True),
----> 9 Field('unicity', type = 'string', compute = lambda x:
x.triggerName + str(x.triggerValue), unique = True),
10 format = '%(triggerName)s - %(triggerValue)s',
11 migrate = settings.migrate)
/Users/joseph/Documents/workspace/web2py/gluon/dal.pyc in
__getattr__(self, key)
3061
3062 def __getattr__(self, key):
-> 3063 return dict.__getitem__(self,key)
3064
3065 def __setattr__(self, key, value):
KeyError: 'triggerName'
Could anyone help me out of this one ? :)