Access ORM models from different classes in Odoo/OpenERP - python

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

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.

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

Which base class to inherit if it's a common class in odoo9

I'm using odoo9 for my project.
In my project, there is a common excel handling class fill in the excel template and output result, and here is my question: which base class should it inherit? models.Model or http.Controller, or nothing?
If you are going to create odoo normal module then you must create models.Model.
If you are going to create odoo module which will handle post or get request from web service then you must use controller.
If you are going to create odoo module for other module, and this module is wizard then you must use transient model and etc.
Also if you need you can make simple class and use in your module but with your question I cant tell you more

Django - get instances of base model as corresponding proxy models

I have a base Django model, and proxy models that subclass it. They override all methods. I need to iterate over all the instances of the base model (i.e. for i in BaseModel.objects.all()), but be able to call the methods of their corresponding proxy classes instead of the placeholder methods declared in the base class.
How do I approach this? I actually have a model field which can determine which proxy model corresponds to each particular instance. Maybe I can make use of it and cast the base class into the subclass somehow? I'm at a loss.
EDIT: I've had a look at this question and have managed to change the class by writing to self.__class__. However, is that safe to use with Django?
proxymodels = {"Foo": FooModel, "Bar": BarModel}
for o in BaseModel.objects.all():
proxymodels[o.type].method_name(o, *args, **kwargs)
The methods are called on the proxy models (the classes), passing the BaseModel instances as first argument plus any additional arguments you want to pass. That way the methods are called as if they were called on an instance of a proxy model.
PS: re-assigning self.__class__ seems very hackish to me.

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