This question has been flagged
2 Replies
9359 Views

I usually create a new Field by using the Debugging Mode and then clicking "Edit FormView" when I am on the appropriate form and writing into the code something like:

<field name="x_delivery_date"/>. 

Also I can show it afterwards on the printed report like this:

<div name="x_delivery_date" t-if="doc.x_delivery_date">    
<strong>Delivery Date:</strong>   
 <p t-field="doc.x_delivery_date"/>
</div>

But how do I display a field (commitment_date), which is available in the model (sale.order) in another models formview (account.invoice)? I guess that I have to use object relations or related field, but I don't know how. I hope somebody can help me. Many thanks in advance.

Avatar
Discard
Best Answer

@Saitam

Try this, it works! I created 'commitment_date' field in 'sale.order' and added it to 'account.invoice':

class AddCommitmentDate(models.Model):
    _inherit = 'sale.order'
    commitment_date = fields.Date('Commitment Date')

class AddToAccountInvoice(models.Model):
    _inherit = 'account.invoice'
    sale_order_id = fields.Many2one('sale.order', 'Orders')
    commitment_date = fields.Date(related='sale_order_id.commitment_date')
<record id="test_commitment_field_id" model="ir.ui.view">        
    <field name="name">Add commitment field</field>
    <field name="model">account.invoice</field>
    <field name="inherit_id" ref="account.invoice_form"/>
    <field name="arch" type="xml">
        <field name="date_invoice" position="after">
            <field name="commitment_date"/>
        </field>
    </field>
</record>


 

Avatar
Discard
Author Best Answer

@ Art

It doesn't work like this. There is no connection between sale order and account invoice. If I add the following line to account_invoice.py I get a 500 Internal Server Error:
`commitment_date = fields.Date(related='sale_order.commitment_date')`

I am writing this as an answer and not as a comment, because I am new to this forum and I only can comment with 8 carma points but have only gotten 7 carma points with the registration here (which makes absolutely no sense!) .


This my model:

class AccountInvoice(models.Model):
    _name = "account.invoice"
    _inherit = ['mail.thread']       
    _description = "Invoice"       
    _order = "date_invoice desc, number desc, id desc"        
    
    commitment_date = fields.Date(related='sale_order.commitment_date')    
    
    ...

And here is my view code, but the Odoo server doesn't start after the modificated model, so I don't even know if the view code would work or not:

<div name="commitment_date" class="col-xs-3">                    
<strong>Date:</strong>                   
<p t-field="o.sale_order.commitment_date"/>               
</div>


Avatar
Discard

show us your model and view code.

I cant' get it working yet... trying to understand how related fields work.

So here is a short info from a odoo blogger about related fields: "We have to give/add Many2one(sale.order) field in account.invoice object. We can fill up new Many2one(sale.order) field id when invoice create from Sale order or Delivery order based on our Invoice control policy or Product setups. Afterwards you can declare your new field with related like Many2one(sale.order).validity_date. If you don't want to go with related field then we can override few existing method and fill up Expiration date in account.invoice object."

=======

So I tried to add an existing field (validity_date) from sale.order to account.invoice like this:

class AddToAccountInvoice(models.Model):

_inherit = 'account.invoice'

date_id = fields.Many2one('sale.order', 'My Date')

validity_date = fields.Date(related='date_id.validity_date')

but I get "AssertionError: Document does not comply with schema." So looks like Many2one field needs to be created to deal with this issue, just need to figure out the right way.

Ok, I'm stupid. I was getting this AssertionError because I didn't have <odoo> and <data> tags in my xml view.

I have edited my answer. Hope it'll help you.