Skip to Content
Menu
This question has been flagged

I am writing this code:

class my_class_a(models.Model):

[....]

class my_class_b(models.Model):

    _inherit = "res.partner"

   field_ids = fields.Many2Many(........)


class my_class_c(models.Model):

    _inherit = "account.invoice"

    field_myfield = fields.Char(....)


In this case, I am getting a "key error: account.invoice", but if put my_class_b in a separate module, everything works fine. Even get key error if y separate my_class_b in another file, and import it from __init__.py


Why is this for? 


[....]


Avatar
Discard

Are you sure that your module depends on 'account' module in the __openerp__.py manifest?

what is the use and what is the purpose behind this

what is the use and what is the purpose behind this

Author

There is a typo in the example. The last class is class_c

Author

Nicolas. This was the cause. Thanks! If you want to convert as an answer in order for me to vote it

Two components that should work together, that for some reason I tested they worked fine, only when created as separate modules.. but the  problem was that in the manifest definition, where I should have included account as a dependency, as you see the answer.


Atte:



Ing. Daniel Blanco
Director
Blanco Martín & Asociados
Skype: hdanielb
Twitter: hdanielb


Argentina:
BMyA S.A.
Nicasio 6970. Córdoba
Argüello - Córdoba

Chile: 
Blanco Martín EIRL
Apoquindo 6410 Of 212
Las Condes - Región Metropolitana




2015-03-31 4:13 GMT-03:00 Dep <libukoshym-gmail.com@mail.odoo.com>:

what is the use and what is the purpose behind this

--
Dep
Sent by Odoo Inc. using Odoo about Forum Post Writing two models toghether, in the same module, shows me a key error "account.invoice" (inherited model)

Author Best Answer

@Nicolas PIGANEAU:  

you made the thing! This was the cause! THANKS!

("Are you sure that your module depends on 'account' module in the __openerp__.py manifest? ")

In the merged module there was an empty depends declaration. I assumed that depends declaration was to force the module installation only, and as it was one of the main modules.

Cannot convert your comment as an answer because of karma. But you deserve. Thank you.

Avatar
Discard
Best Answer
  • you're using the same name ( my_class_b ) for the two classes, while you're inheriting from different ones.

Avatar
Discard

I think the _name has nothing to do with the problem. In fact, defining an ORM class with an explicitly different name from the original ORM model has it's own meaning (see https://doc.odoo.com/v6.0/developer/2_5_Objects_Fields_Methods/object_inherit.html/). But, yes, you should not define both class with the same name in the same file as only one will be registered. If you put it in different file, it will have different namespace. A namespace in python is defined, among other things, by the folder and file/module (a module in python is defined as a file) name. See https://docs.python.org/2/tutorial/classes.html#python-scopes-and-namespaces for more on namespace.

thanks @ivan, I know about meaning of _name, etc... but there I don't see sense to use same _name in two classes while inheriting one from "res.partner" and another from "account.invoice", as there is used same class name for both then "_name" will be the same also. it's not problem that "_name" is different from it's parents one(while it may be, regarding requirements), but it's problem that it's not different between these two classes... so if it's absolutely necessary to use same name for both class, then explicitly using "_name" and make them different from each other by the "_name" may help (?)

AFAIK, if _name is not stated, the inheritance will just take the _name attribute from the class which is _inherit-ed. You can check the create_instance method of orm for v7.0 or _build_model of BaseModel for v8.0 to get a clearer picture of how this work. So, it is not OK to use the same class name (my_class_b) because of the Python namespace. It is OK to not explicitly specify the _name attribute if _inherit has been specified (and it will be derived from the _inherit attribute, not the class name, first). It is somewhat OK to explicitly specify _name if all the repercussions has been considered even if _inherit is specified (I wouldn't do this, but you might be able to get away by specifying the same value _name attribute, which will translate to table name if _table_name attribute is not specified).

as noted in the question namespace problem was resolved by putting my_class_b (probably one of them) in the separated module. I mean using same name for the two class in this conditions (putting them in different modules), without violating namespace rules. I tried to give second option, using _name but I see it doesn't gave anything. so I'll remove second one and leave just first note in my answer, as we are agree that using the same name for the two classes in the above code is causing the problem. thanks for your comments @Ivan

Author

It was a typo in the example. Corrected.. but the problem persists

Author

the _name field "IS" in the real code.

Related Posts Replies Views Activity
0
Nov 20
4209
3
Jan 22
50105
1
Oct 24
221
0
Mar 24
337
2
Aug 23
40432