SqlAlchemy inheritance mechanism like Open Object Framework - python

Newbie here! Here is my question: In Open Object Framework that is used by Odoo it is possible for a model to inherit both fields and methods from multiple modules. For example a Product model in the context of a Warehouse Management System does not have the same attributes and functionality as a Product model in the context of a Sales Management System.
Consider a Product model defined in a WMS module and a Product model (please note the same name) defined in a SMS module, where the basic attributes and functionality are defined in the WMS.Product.
Is it possible to achieve such an inheritance mechanism with SqlAlchemy so that when calling create_all the Product model that is created contains all the fields and methods that are defined in every module?

Related

Python data abstraction on top of PeeWee

It's more of a general question but I am trying to implement this using Python on top of Peewee as ORM. What is a decent OO way of abstracting the DB out of a python program?
In Peewee , classes are defined which inherit from peewee.Model and have Peewee fields as attributes.For example:
class Person(peewee.Model):
class Meta:
database = db
name = peewee.TextField()
height = peewee.DecimalField()
In an OO implementation we would like to have methods such as grow(size), die(),.. to be part of the objects. Is it best to build a class on top of these Peewee models to contain such functionality or should this be put in the model itself?
I can remember in a Java EE program that we used to have a DAO (Data access object) and DTO (Data transfer object). The peewee model object is a DAO or can it be both? Is there some sort of pattern that can be applied here?
Peewee is an ActiveRecord ORM, so there's no distinction between the data access and the object representation. This means when you execute queries the data is returned to you as model instances. Given that this is the case, it's common to put methods on the model itself, since you're using it anyways.
Whether you want to build a service layer on top of your models is entirely up to you. If you have mutually-dependent models this may make sense.

when should I write custom methods in model vs model manager/ queryset

when should I write methods in the model itself and when in the model manager?
is it like all methods to get something should be written in manager and others in model
There's a simple difference. Model methods act on a single instance. Manager methods create queries to act on multiple instances.

Where is the declaration of objects of django model?

In django documentation, it say that we can retrieve data entry as below
entry = Entry.objects.get(pk=1)
Entry is a model class in models.py. I tried to find the declaration of objects, but I can't find its declaration in manager.py, just know it is a instance of Manager. So, where is the declaration of objects? Does it represent a set of Entry instances?
When you define model, you extend Model class from django.db.models module.
It will provide default model manager in objects property.
If you want to define custom model manager, you can do it by subclassing django.db.models.Manager class.
Look at the docs how to do that: https://docs.djangoproject.com/en/1.11/topics/db/managers/
Add methods to custom model managers to if you want to manipulate with the collection of data. If you manipulate with single row, add method to your model.
It's defined in ModelBase, which is the metaclass for model classes. See https://github.com/django/django/blob/master/django/db/models/base.py#L360

Access ORM models from different classes in Odoo/OpenERP

I am aware that you can get a reference to an existing model from within another model by using self.pool.get('my_model')
My question is, how can I get a reference to a model from a Python class that does NOT extend 'Model'?
In that case you will have to import the class like a normal Python class, writing in your .py file:
from your_path_class import YourClass
And then you will be able to work with it:
Yourclass.any_method()
By the way, self.pool.get('your model') in Odoo 8 API is self.env['your_model'].
It's pretty basic and simple any python class can be called from it's name space, so call your class from namespace and instanciate the class.
Even Model class or any class inherited from Model can be called and instanciated like this.
Self.pool is just orm cache to access framework persistent layer.
Bests

Subclassing Satchmo's Category model, but then getting the error "'Manager' object has no attribute 'root_categories'"

I'm using Satchmo as part of a website I'm currently building. At the moment I'm trying add more functions to the Satchmo Category class, but obviously I'm not going to make any changes to the Satchmo files. So, I thought that subclassing the Category class would give me a new class which contains all the Satchmo Category properties and methods while allowing me to add my own. However, either Python subclassing doesn't work like that, or I am doing it wrong. Here is the code I'm using to subclass Category:
from product.models import Category
class MyCategory(Category):
""" additional functions to pull data from the Satchmo store app """
One of the methods I can normally use from the Category class is:
Category.objects.root_categories()
however, when I try to access
MyCategory.objects.root_categories()
I get the following error:
AttributeError: 'Manager' object has no attribute 'root_categories'
Can anyone point me in the right direction for solving this?
You should read the docs on custom managers and model inheritance.
In any case, you should probably be defining the MyCategory class as a Proxy model, which does inherit the parent class's Manager.

Categories