问题
I am working on a module that will print some values related to the invoice. The only two things that are missing in the Accounting Model are fields:
-vat
-code (Country Code)
I have successfully added vat field. However, get an error when trying to bring the "code" field. My py code is as follows:
from openerp import models, fields
class CountryCodeInvoice(models.Model):
# where to place new fields
_inherit = 'account.invoice'
# getting country code to the accounting model
code = fields.Char(string='Country Code', related='res_country.code')
class AccountInvoiceInherited(models.Model):
# where to place new fields
_inherit = 'account.invoice'
# getting the vat field to accounting model
vat = fields.Char(string='vat', related='partner_id.vat')
I have definitely messed up this part:
related='res_country.code'
This is the final result I am trying to get:
Do you know any tutorials that explain how to work with related fields? Official documentation does not go very deep...
回答1:
Related fields base on a relation on the model you're working on. Usually these fields are Many2one
fields. You already used one for vat
: partner_id
which is a Many2one
relation to model res.partner
.
You can relate to other fields of this relation, like in your example the vat of the invoice partner. You have to use dot-notation like in the most object oriented languages.
But the chain doesn't stop on the first piece. So you can relate on much "deeper" relations. For example your country code:
code = fields.Char(string='Country Code', related='partner_id.country_id.code')
Again it's partner_id
the chain begins with. But the country code lays deeper in the relation chain. res.partner
has a Many2one
relation to model res.country
which holds the code. Just use dot-notation to get to it.
来源:https://stackoverflow.com/questions/50911599/odoo-how-to-add-code-field-to-accounting-model