'Instantiate a given model in the registry.
This method creates or extends a "registry" class for the given model.
This "registry" class carries inferred model metadata, and inherits (in
the Python sense) from all classes that define the model, and possibly
other registry classes.'

@classmethod
def _build_model(cls, pool, cr):

    cls._local_constraints = cls.__dict__.get('_constraints', [])
    cls._local_sql_constraints = cls.__dict__.get('_sql_constraints', [])
    parents = cls._inherit
    parents = ([parents] if isinstance(parents, basestring) else (parents or []))
    name = (cls._name or ((len(parents) == 1) and parents[0]) or cls.__name__)
    if (name != 'base'):
        parents = (list(parents) + ['base'])
    if (name in parents):
        if (name not in pool):
            raise TypeError(('Model    %r    does    not    exist    in    registry.' % name))
        ModelClass = pool[name]
        ModelClass._build_model_check_base(cls)
        check_parent = ModelClass._build_model_check_parent
    else:
        ModelClass = type(name, (BaseModel,), {'_name': name, '_register': False, '_original_module': cls._module, '_inherit_children': OrderedSet(), '_inherits_children': set(), '_fields': {}})
        check_parent = cls._build_model_check_parent
    bases = LastOrderedSet([cls])
    for parent in parents:
        if (parent not in pool):
            raise TypeError(('Model    %r    inherits    from    non-existing    model    %r.' % (name, parent)))
        parent_class = pool[parent]
        if (parent == name):
            for base in parent_class.__bases__:
                bases.add(base)
        else:
            check_parent(cls, parent_class)
            bases.add(parent_class)
            parent_class._inherit_children.add(name)
    ModelClass.__bases__ = tuple(bases)
    ModelClass._build_model_attributes(pool)
    check_pg_name(ModelClass._table)
    if ModelClass._transient:
        assert ModelClass._log_access, 'TransientModels    must    have    log_access    turned    on,    in    order    to    implement    their    access    rights    policy'
    ModelClass.pool = pool
    pool[name] = ModelClass
    model = object.__new__(ModelClass)
    model.__init__(pool, cr)
    return ModelClass
