db.define_table(
auth.settings.table_user_name,
[...]
Field('calculated_field', requires=IS_NOT_EMPTY(),
compute=lambda(r): calculate_field(r)),
dct_new_user = {...}
new_user = db.auth_user.validate_and_insert(**dct_new_user)
<Row {'errors': {'calculated_field': 'Enter a value'}, 'id': None}>
Hi Alfonso,
I think the original reason was that sometimes I would end up with a computed field without a value. Inserts should automatically compute rows, but for some reason that wasn't happening here.
Hi Chris, why do you use requires IS_NOT_EMPTY in a computed field?, if you do, you have to supply a value before is computed, isnt it?
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/Kqfg-MG5K44/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thanks for the info Anthony! I'll try one of those approaches.
Why is validation run before compute?
Thanks for the info Anthony! I'll try one of those approaches.
Why is validation run before compute?
Notice thatrequires=...
is enforced at the level of forms,required=True
is enforced at the level of the DAL (insert), whilenotnull
,unique
andondelete
are enforced at the level of the database. While they sometimes may seem redundant, it is important to maintain the distinction when programming with the DAL.
Uh oh...I just removed requires from the computed field and reran my unit tests, and now validate_and_insert inserts but the computed field isn't calculated at all! I tried insert and it does the computation.
Is there some reason validate_and_insert would not compute where insert does?
dct_new_user = { fields here }
# new_user = db.auth_user.insert(**dct_new_user)
new_user = db.auth_user.validate_and_insert(**dct_new_user)
# DB
db.define_table('story',
Field('title',
length=512,
widget=lambda field, value: SQLFORM.widgets.string.widget(field,
value,
_size=40),
requires=[IS_NOT_EMPTY(), IS_LENGTH(minsize=1, maxsize=512)]),
Field('titleAsSlug',
compute=lambda(r): urls.convert_to_slug(r['title'])),
Field('readURL', unique=True, label=T('URL'), required=False,
widget=lambda field, value:
SQLFORM.widgets.string.widget(field,
value,
_size=60,
_placeholder='http://www.example.com')),
)
# Test
class TestModels(unittest.TestCase):
def testStoryNewCreate(self):
dct_new_story = {
"title": "Unit Test"}
new_story = db.story.validate_and_insert(**dct_new_story)
self.assertFalse(new_story["errors"], "Error inserting a new story: " + str(new_story))
Need to see the fields.
db.story.titleAsSlug.requires = db.story.readURL.requires = None
def __call__(self, value):
value = to_native(str(value))