This directory is particularly important due to some characteristics: I warn you to pay attention to the first one: files are executed in alphabetical order. Some newbies have dificulties with that, and tend to think this is strange. I did it, until I concluded this is the most intuitive way to things work. It gives me freedom to organize my app. However, to avoid some mistakes this way can bring to your app, we need to adopt a clear and fool proof convetion to name I use this simple and efficient structure: I rename the automatically created I also rename With these modified names, I’m sure that all the code I’ll develop will be executed after the scaffolding app web2py generated to me. From that point I write Validators are written in When I have many tables, I can separate them in various scripts, i.e, Here, your creativity can fly, but remeber some principles: Explicit is better than implicit.
Total control over your models
models dir.
models directory files ares executed in alphabetical order;models files.
models/0_db.pymodels/1_menu.pymodels/5_models.pymodels/5_validators.pydb.py file to 0_db.py. So, I guarantee all basic definitions are executed before anything else. The DAL object, who provides all data access funcionalities, is created in this script.menu.py to 1_menu.py forcing all menu definitions to be executed after DAL object instantiation.models/5_models.py with my table definitions. Nothing butdb.define_table() commands.models/5_validators.py. But, why separated from model definitions? First, because web2py recommends you don’t mix validators with db.define_table(). Second, because at this point I’m certain all tables are already created and all cross references between them will work with no need to adjust definition sequences. Sometimes it messes my scripts.5_models_accounts.py, 5_models_finances.py, etc. And, the same way, their validators.
Simple is better than complex.
Flat is better than nested.
Readabilty counts.
In the face of ambiguity, refuse the temptation to guess.
Category Archives: Trucos de Web2py
how to generate a many-to-many-relationship FORM in web2py?
python – how to generate a many-to-many-relationship FORM in web2py? – Stack Overflow
|
Do I need a custom validator? Do I need a custom widget? If this helps to clear the problem, the relationship is between I would like to add a multi-select box in the “add member” form (that I generate using SQLFORM). Thanks |
||||
|
feedback
|
Modify CRUD Form in web2py before sending to view
python – Modify CRUD Form in web2py before sending to view – Stack Overflow
|
I cannot seem to find a way to modify a form that has been created via:
Since I am using foreign keys in my table, the auto-generated form only allows an integer (which represents the foreign primary key), but what I want to be able to do is enter whatever data type the foreign data field requires (rather than just the identifier). Is there an easy way to tell the create() function to use the foreign table’s data type rather than the primary table’s data type (which is the auto-incrementing primary key)?
|
|||
|
feedback
|
|
You can use database validators to it. It will show a select box with the values from foreign table: (from http://web2py.com/book/default/chapter/07?search=requires#Database-Validators): IS_IN_DBConsider the following tables and requirement:
It is enforced at the level of dog INSERT/UPDATE/DELETE forms. It requires that a dog.owner be a valid id in the field person.id in the database db. Because of this validator, the dog.owner field is represented as a dropbox. The third argument of the validator is a string that describes the elements in the dropbox. In the example you want to see the person %(name)s instead of the person %(id)s. %(…)s is replaced by the value of the field in brackets for each record. The zero option works very much like for the If you want the field validated, but you do not want a dropbox, you must put the validator in a list.
The first argument of the validator can be a database connection or a DAL Set, as in Occasionally you want the drop-box (so you do not want to use the list syntax above) yet you want to use additional validators. For this purpose the IS_IN_DB validator takes an extra argument _and that can point to a list of other validators applied if the validated value passes the IS_IN_DB validation. For example to validate all dog owners in db that are not in a subset:
IS_IN_DB and TaggingThe IS_IN_DB validator has an optional attribute multiple=False. If set to True multiple values can be stored in one field. This field should be of type list:reference as discussed in Chapter 6. An explicit example of tagging is discussed there. multiple references are handled automatically in create and update forms, but they are transparent to the DAL. We strongly suggest using the jQuery multiselect plugin to render multiple fields. |
||||
|

