Can do, here's an example:
# 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)) 

The readURL field generates an error that the entry is found in the 
database. Unique=True is a DB constraint according to the web2py manual and 
NULLs in PostgreSQL and SQL in general don't violate the unique constraint. 
Web2py attaches an IS_NOT_IN_DB validator that converts None to "None" 
instead of null, which only works until there is a row entitled 'None'.
The titleAsSlug field is set to an empty string by the validator, which is 
perhaps why validate_and_insert doesn't compute it where insert does.


On Thursday, April 20, 2017 at 11:05:02 PM UTC-4, Anthony wrote:
>
> Need to see the fields.

-- 
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 the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to