odoo inherit a model and "key error [my_database]" - odoo

I want to try to learn Odoo _inherit. But I have this error right now. And I couldn't solve the problem.
my module name is stock.info and I want the inherit product.template model.
but this error came.
TypeError: Many2many fields stock.info.taxes_id and product.template.taxes_id use the same table and columns
I added product.template to my depends in manifest too. Any advice?

Reminder for future questions: You should provide code for your Problem and not only an error message.
Like the error says you have 2 many2many fields which refers to the same table and columns.
You have to know that many2many fields in Odoo generate a mapping table in the database with two columns which are foreign keys to tables of the referenced models. The name of the mapping table and the column names are automatically generated by default. So if the automatic generation for your field creates the same name for table/columns which are already in use, you will get this error.
To solve this, you can assign additional attributes when defining your field.
So your field could like like this:
taxes_ids = fields.Many2many(relation='stock_info_account_tax_rel', column1='product_id', column2='tax_id')
(remember that many2many fields should be named with suffix _ids and not _id)
Take a look at the documentation for further informations: odoo.fields.Many2many
Parameters:
comodel_name – name of the target model (string) mandatory
except in the case of related or extended fields
relation (str) – optional name of the table that stores the relation
in the database
column1 (str) – optional name of the column referring to “these”
records in the table relation
column2 (str) – optional name of the column referring to “those”
records in the table relation
The attributes relation, column1 and column2 are optional. If not
given, names are automatically generated from model names, provided
model_name and comodel_name are different!
Note that having several fields with implicit relation parameters on a
given model with the same comodel is not accepted by the ORM, since
those field would use the same table. The ORM prevents two many2many
fields to use the same relation parameters, except if
both fields use the same model, comodel, and relation parameters are
explicit; or
at least one field belongs to a model with _auto = False.

Related

Why related fields use Write function

_name = "my.table"
building_id = fields.Many2one('building', related='floor_id.building_id', readonly=False)
floor_id = fields.Many2one('building.floor')
A user with the read access to 'building' and 'building.floor' tables, tries to create a record in "my.table" If the user chooses building_id and floor_id together an error occurs. The error says that my user has no access to write 'building.floor' table. My question is: why a related field use the write function, what is the difference between the compute and related in this scenario?
Related fields are very simple computed fields. So simple they can be "implemented" with one parameter on field definition. Odoo has generic methods for those fields. For example a lot of developers don't write inverse methods for computed fields, which inverse the compute method, because the simply don't need it. But without it and without storing the computed field, Odoo sets the field readonly.
Related fields have a generic inverse method. In your case changing building_id when there was already a floor_id chosen, Odoo will write the building_id on that floor_id.building_id, because that's how related fields work (i know that's not the best explanation).
The user obviously has no write/update rights on builiding.floor model and that's why there will be the access error message in the end, because Odoo wants to write the new building on the floor.
Seems to me you want to filter the floors by buildings, but you shouldn't use a related field for that. Just put a domain on floor_id which filters by the chosen building_id:
floor_id = fields.Many2one('building.floor', domain="[('building_id', '=?', building_id)]")
You could also use domain operator =, but =? will show all floors when no building was set yet.

Ms Access SQL Limit control by previous field value

In a recipe database I have two tables. One has the ingredients of every recipe [Recipe_ingr] and the other the available measures and weight for every ingredient [Weight2].
When I input a new ingredient for a recipe, I would like to be able to choose the available units for only that specific food.
I have tried with this expression in the control field but it prompts me to choose first, and then the options remain the same for all the records, not changing dinamically according to the record ingredient code.
SELECT [Weight2].[Msre_Desc], [Weight2].[Gm_Wgt] FROM Weight2 WHERE Weight2.NDB_No Like Recipe_Ingr.NDB_No ORDER BY [Msre_Desc], [Gm_Wgt];
Picture of my tables
Update:
I tried the syntax change suggested by June9 but still the control doesn't update automatically with every record as you can see in this picture: Table
Suggest you name controls different from fields they are bound to, like tbxNDB. The SQL needs to reference a field or control that is on the form. Also, LIKE operator without wildcard accomplishes nothing that an = sign wouldn't. Also recommend not using exactly same name for fields in multiple tables.
If you use that SQL statement in combobox RowSource, try:
SELECT Msre_Desc, Gm_Wgt FROM Weight2 WHERE NDB_No = [tbxNDB] ORDER BY Msre_Desc, Gm_Wgt;
You want to save Msre_Desc as foreign key, not a record id generated by autonumber?

How to make a constraint on a fields

I want to put a control check on a field (TIN) so that everytime I create a new customer, the TIN should be unique. If another customer has that TIN, an error message must show up.
I tried this syntax:
_sql_constraints=[('uniq_vat', 'UNIQUE(self.env.vat)',
'It already exists another company with the same TIN!')]
I'm using odoo 10.
Constrains can be of two types.
Application Constraints
Database Constraints
Database Constraints
Database constraints will add validation at database level while you upgrade that module. Database constraints is list of tuples, in which tuple contains three arguments.
_sql_constraints = [
('constrain name', 'unique(field1, field2)', 'error message which you want to raise on constrains violation'),
('constrain name', 'constrains defination', 'error message which you want to raise on constrains violation'),
]
Constraints name
Constraints, like unique, check
unique constraints can be applied to many columns.
Error message
Example:
_sql_constraints = [
('uniq_vat', 'unique(vat)', 'It already exists another company with the same TIN!'),
]
Multiple database constraints can be added together.
Application Constraints
Application constraints is used to fire custom validation at the time of record add, update and delete. In short your custom method will be called if any changes happen with record.
How to define constrains in code.
#api.constrains('field1','field2'....)
Constrains can be applied to multiple fields together, you can also define it separately.
#api.constrains('vat')
def check_vatnumber(self):
for record in self:
obj = self.search([('vat','=',record.vat),('id','!=',record.id)])
if obj:
raise Warning("Warning", "It already exists another company with the same TIN!")
_sql_constraints = [('unique_tin_no', 'unique(field_name)', 'It already exists another company with the same TIN!')]
Small change is in your line and your constrain work correctly
_sql_constraints=[('uniq_vat', 'unique(vat)', 'It already exists another company with the same TIN!')]
you just need to put "vat" in place of "self.env.vat" sql_constrains just needs field name which is going to apply on DB table underneath.

How to add a many2one relation to a custom model

I try to add to an order a new attribute that relates to a custom many2one relation. The goal is to choose for each order one specific contract condition. I would like to manage those contract conditions in the database, so that I can easily manage them.
I sort of got far. I can edit those conditions, assign them and get them properly printed. However, on the sale-order form they get displayed in a weird way. Instead of the descirption-text of the condition, I see sort of a description of the associated record. So my question is, how to show the proper description attribute. See here:
Below I added a few screenshots that explain the type of changes that I did.
custom data structure:
many2one relation from sale.order to custom structure:
views for custom structure:
reference from order form, which is displayed oddly
Define _rec_name into your class.
_rec_name = 'x_condition'
It's because it will looking for name field into your custom model when you add many2one field for that model, when you define _rec_name it will take that field value.
Try to use x_name instead of x_condition for field name

Django queries: how to annotate with a filtered count?

Suppose I have a Book model with a language field and a foreign key to a Publisher model.
Currently I use a Count annotation in a custom Publisher manager to allow me to add to the admin a sortable column with the number of books by each publisher. (see How to add a sortable count column to the Django admin of a model with a many-to-one relation? )
My problem now is that I need to have a different column count for the books published in each language.
Is there any way to make the annotation subject to a filter of the related model?
You can use the double underscore __ for this purpose. Something like this (snippet taken from question linked to by OP):
class PublisherManager(models.Manager):
def get_query_set(self):
return super(PublisherManager,self).get_query_set().annotate(lang_count=Count('book__language'))