开发者

Meta programming in OpenERP

Here a way to develop OpenERP

class stock_incoterms(osv.osv):

_name开发者_开发问答 = "stock.incoterms"
_description = "Incoterms"
_columns = {
    'name': fields.char('Name', size=64, required=True),
    'code': fields.char('Code', size=3, required=True),
    'active': fields.boolean('Active'),
}
_defaults = {
    'active': lambda *a: True,
}

stock_incoterms()

This is typical OpenERP model and map to table name stock_incoterms.

The columns property defines columns for the table The default property defines default value for each column.

My questions are:

How to implement this in Python?

When running stock_incoterms(), OpenERP will read this model, generate the table with these defined columns.

After that, suppose I created a record in this table, then I ask give me the model of this class, ex

stock_incoterm = stock_incoterms.get(1) ( record id in db)

Now i can access

stock_incoterm.name

stock_incoterm.code

........

This way of defining model is kind of Meta Programming but I don't know how to write it in Python.

Could anyone guide me the details behind or some good links about meta programming in Python?


The OpenERP developer book is the best place to start looking. It sounds like you want to add a new table called stock_incoterm. I would recommend you start a new module and add the table to it, along with a screen and some menu items to edit the table. The developer book has a chapter on developing modules.

Once you've got all that set up, your code to access fields on a record would look something like this:

class stock_incoterms(osv.osv):
    # ...

    def do_something(self, cr, uid, ids):
        for stock_incoterm in self.browse(cr, uid, ids):
            name = stock_incoterm.name

            # ...


Study OpenERP's code, then :-)

Other nice starting points include:

  • Logilab's yams module and Cubicweb's ORM
  • Tools like SQLAlchemy

Things boil down to using Python's introspection facilities to find out the structure of the objects provided by the user and converting that structure to e.g. SQL table definitions and queries.

In yams, a Schema object is created by the user defined classes which derive from EntityType which has the metadefinition metaclass. This metaclass lists the class attributes of the EntityType and stores them in various internal fields. Then the schema2sql module can be used to serialize the schema as a set of SQL tables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜