El 15/01/14 23:05, Frédéric Marchal ha escrit:
On Tue, Jan 14, 2014 at 10:54 PM, Sergi Almacellas Abellana <se...@koolpi.com <mailto:se...@koolpi.com>> wrote:

    >Is there a base model that I could inherit from or any other means in
    >order
    >to add fields, methods, ... to all models at once in a module ?

    AFAIK you must inherit models one by one.

    We can give more help if you explain your prupose.


Well I can think of many use cases but concretely, I'd like to add one or more fields to all models actually present and all the one that will be installed further (hence I was hoping that tryton models were built out of a "root.model" alterable per database that all other models would inherit from)

Then I would use one of those added field to "tag" every object of every models that I want to expose to my host application. It would also allow me to overload read(), search(), ... on all models in order to update the domain before calling super() according to an opt-in in the context. Then I would overload fields_view_get() 's "arch" for all models in order to "monkey patch" the form views and add the "tag" field in them.

But I have other use cases, eg : another field would be a json represented cache of the tagged objects by overloading create() and write() of all models, allowing one to define a "schema" of the json cache so it can fetch some relations too, storing it in a json_cache field or memcached.

Well, you see... that kind of stuff. Having the possibility to overload the lower level model layer seems a powerful feature to me. You can really bend the system to your needs and design your application storage the way you want. I guess you might propose alternative to each of my use cases but it would be so easy if it just worked using the traditional inheritance scheme of tryton.
So you may need to monkey patch model, modelstorage or modelsql [1] in order to achive your needs. But i will prefer you create a class that inherits from model (Taggable, or JsonCachable) and inherit your modules from this class. Explicit is better than implicit :)

[1] http://hg.tryton.org/trytond/file/8d3e9acd35b3/trytond/model

--
Sergi Almacellas Abellana
www.koolpi.com
Twitter: @pokoli_srk

Reply via email to