Changes between Version 11 and Version 12 of S3/S3Model


Ignore:
Timestamp:
01/17/14 12:00:33 (11 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TabularUnified S3/S3Model

    v11 v12  
    176176These functions should not be overwritten in the subclass.
    177177
     178== current.s3db ==
     179
     180{{{current.s3db}}} is a global, empty instance of the S3Model class that loads tables, functions and variables from other models on demand.
     181
     182=== Loading Tables, Functions and Variables from Models ===
     183
     184{{{current.s3db}}} allows easy access to names using the ''attribute''-notation:
     185
     186Loading a table:
     187
     188{{{#!python
     189table = current.s3db.my_table
     190}}}
     191
     192Loading a function:
     193
     194{{{#!python
     195my_function = current.s3db.my_function
     196}}}
     197
     198If you have the name in a variable, you can use the ''item''-notation instead:
     199
     200{{{#!python
     201tablename = "my_table"
     202table = s3db[tablename]
     203}}}
     204
     205  '''Note:''' The attribute- or item-notations will raise an {{{AttributeError}}} if the name can not be found (e.g. when the respective module is disabled).
     206
     207To avoid exceptions due to disabled modules, one can use the {{{table()}}} function to access tables:
     208
     209{{{#!python
     210# Returns None if "my_table" is not found
     211table = current.s3db.table("my_table")
     212}}}
     213
     214  '''Note:''' {{{s3db.table()}}} will also return functions or variables with the specified name.
     215
     216To limit the lookup to tables, use {{{db_only}}}:
     217
     218{{{#!python
     219# Returns only tables, but not functions or variables
     220table = current.s3db.table("my_table", db_only=True)
     221}}}
     222
     223To only lookup functions and variables, but not tables, you can use the {{{get()}}} function:
     224
     225{{{#!python
     226# Returns only functions or variables, but not tables
     227my_function = current.s3db.get("my_function")
     228}}}
     229
     230=== Utility Functions ===
     231
     232{{{current.s3db}}} also provides the {{{S3Model}}} utility functions, e.g.:
     233
     234{{{#!python
     235# Change a table configuration
     236current.s3db.configure("my_table", list_fields=["id", "name", "other_field"])
     237}}}
     238
     239   '''Note:''': {{{s3db.get_config()}}} does not load the respective model, so unless you have loaded the model before, you can not access its configuration settings!
     240
     241=== Constructing Resources ===
     242
     243{{{current.s3db}}} also provides a method to define a resource (see [wiki:S3/S3Resource]):
     244
     245{{{#!python
     246# Define a resource
     247resource = current.s3db.resource("my_table")
     248
     249# Limit to certain record IDs
     250resource = current.s3db.resource("my_table", id=[1, 3, 7])
     251
     252# Use a filter
     253from s3 import S3FieldSelector
     254resource = current.s3db.resource("my_table", filter=S3FieldSelector("id").belongs([1, 3, 7]))
     255
     256# Apply a URL filter
     257resource = current.s3db.resource("my_table", vars={"~.id__belongs": "1, 3, 7"})
     258}}}
     259
    178260== Concepts ==
    179261