Odoo 10 Change form view Many2one res.partner - odoo

I have a custom odoo app. In my model I have a Many2One form the type res.partner.
If I display the field <field name="projectmanager" domain="[('partner_type', '=', 'Manager')]"/> in a form view, I get the name, address, city and country.
How can I hind the address and display only the name?
The model:
class CalamityCalamity(models.Model):
_name = 'calamity.calamity'
_inherit = ['mail.thread']
_description = 'Schadelijsten'
_order = "projectnr"
_rec_name = "projectnr"
projectnr = fields.Char(string='Projectnummer')
projectmanager = fields.Many2one('res.partner', ondelete='set null', string="Projectmanager", index=True, domain=[('partner_type','=','Manager')])

Many2one field always open the default form view but you can change this And define witch form using xml id
<field name="projectmanager" context="{'form_view_ref': 'module_name.form_id'}"/>
Just create a new form that display what you need exactly

did you tried this
<field name="projectmanager" options='{"widget": "contact", "fields": ["name"]}'/>

try to replace "res.partner" with "partner_id.field_name".
projectmanager = fields.Many2one('partner_id.name', ondelete='set null', string="Projectmanager", index=True, domain=[('partner_type','=','Manager')])
Hope this will help you.

Related

I am trying to inherit and add one2many fields in res.partner model . It shows errror : Invalid field 'same_vat_partner_id' on model 'vehicle.brand'

inherited " res.partner " and added a page (editable tree) in notebook section, but when clicking on "Add a line" it is showing below error:
Invalid field 'same_vat_partner_id' on model 'vehicle.brand'
My code to inherit res.partner and add One2many fields in it :
from odoo import api, fields, models
class CustomContacts(models.Model):
_inherit = "res.partner"
x_brand_ids = fields.Many2many('res.partner.line', 'x_brand_id', string="Brand Name")
x_model_ids = fields.One2many('res.partner.line', 'x_model_id', string="Model Name")
class CustomContactsPage(models.Model):
_name = "res.partner.line"
x_brand_id = fields.Many2one('vehicle.brand', string="Brand Name")
x_model_id = fields.Many2one('vehicle.model', string="Model Name")
My code of vehicle.brand model :
from odoo import api, fields, models
class BrandCreate(models.Model):
_name = "vehicle.brand"
_description = "Customer"
name = fields.Char(string='Brand Name', required=True)
My code of vehicle.model model :
from odoo import api, fields, models
class ModelName(models.Model):
_name = "vehicle.model"
_description = "Models"
name = fields.Char(string='Model Name', required=True)
My code of view :
<?xml version="1.0" encoding="utf-8"?>
<record id="view_partner_form_inherited" model="ir.ui.view">
<field name="name">res.partner.inherited</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//page[#name='internal_notes']" position="after">
<page string="Vehicle Details">
<field name="x_brand_ids"></field>
<field name="x_model_ids"></field>
</page>
</xpath>
</field>
</record>
Getting view inside view as mentioned in below images :
view 1
view 2
You needs to understand the Many2many and One2many fields.
For many2many you check the required argument is the model name only. and other are optional. (Filling Many2many field (odoo 8))
x_brand_ids = fields.Many2many('res.partner.line', string="Brand Name")
For adding One2many we need many2one with that same model.
x_model_ids = fields.One2many('res.partner.line', 'x_model_id', string="Model Name")
For this you need x_model_id Many2one of res.partner field under res.partner.line
You need to like below:
class CustomContacts(models.Model):
_inherit = "res.partner"
x_brand_ids = fields.Many2many('res.partner.line', string="Brand Name")
x_model_ids = fields.One2many('res.partner.line', 'x_model_id', string="Model Name")
class CustomContactsPage(models.Model):
_name = "res.partner.line"
x_brand_id = fields.Many2one('vehicle.brand', string="Brand Name")
x_model_id = fields.Many2one('res.partner', string="Model Name")
When using a One2many field the inverse_name (x_model_id in your example) is equal to the current record and Odoo tries to recompute the partner vat using vehicle.model record in place of a res.partner record and this is why you are getting that error.
To fix it just define the inverse name in vehicle.brand model and use it in x_model_ids field instead of x_model_id.
Example:
class CustomContacts(models.Model):
_inherit = "res.partner"
x_model_ids = fields.One2many('res.partner.line', 'model_id', string="Model Name")
class CustomContactsPage(models.Model):
_name = "res.partner.line"
model_id = fields.Many2one('res.partner')
Edit:
If you want to edit the One2many field in place, define it inside the form view like following:
<field name="x_model_ids">
<tree editable="bottom">
</tree>
</field>
For the Many2many fields, Odoo will open a dialog to select or create vehicle brands.

How to filtred when I have different models

This is the .py:
class Travsup(models.Model):
_name = 'facturation.travsup'
attachment_id = fields.Many2one('facturation.attachement')
ouvrage_id = fields.Many2one('facturation.ouvrage',domain="?????")
class Attachement(models.Model):
_name = 'facturation.attachement'
travSup_ids = fields.One2many('facturation.travsup','attachment_id')
contrat_id = fields.Many2one('facturation.contrat',string='Contrat',required=True)
class Contrat(models.Model):
_name = 'facturation.contrat'
ouvrage_ids = fields.One2many('facturation.ouvrage', 'contrat_id', string='Ouvrages')
class Ouvrage(models.Model):
_name = 'facturation.ouvrage'
contrat_id = fields.Many2one('facturation.contrat', string='Contrat')
How to filtred ouvrage, when i change contrat_id of attachement model i get all
ouvrage of model ouvrage of the same contrat_id of ouvrage model.
if Attachement.contrat_id == Ouvrage.contrat_id then travSup_ids.ouvrage_id = Ouvrage.id
You can specify the domain like this in your form view:
<!-- this field must appear in the form so we can use it in domain -->
<field name="contrat_id"/>
<field name="travSup_ids">
<tree....>
<field name="ouvrage_id" domain="[('contrat_id', '=', parent.contrat_id)]/>
</tree>
<form>
....
....
<field name="ouvrage_id" domain="[('contrat_id', '=', parent.contrat_id)]/>
</form>
</field>
In embaded form or tree you can access the field of the current form by
using parent.field_name

Odoo 10: show field of model "sale.order" in form view of "account.invoice"

I usually create a new Database Structure Field by using the Debugging Mode, then Edit FormView and writing e.g. <field name="x_delivery_date"/>. Also I can show it later 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.
You can use related fields for that. You have to add two fields to account.invoice to do it.
class AccountInvoice(models.Model):
_inherit = "account.invoice"
order_id = fields.Many2one('sale.order', 'Related_order')
commitment_date = fields.Date(related='order_id.commitment_date')
Then you can use the commitment_date fields in account.invoice forms. The value of the field in sale.order will be reflected on the form right away. But be aware that changing the value of that field will change the value of that field on the sale.order as well.
EDIT
For reports just use the field like it is a regular field of account.invoice (so doc.commitment_date)
First you need to add a many2one field in account.invoice
class account_invoice(osv.osv):
_inherit = "account.invoice"
_columns = {
'source_id':fields.many2one('sale.order','Source')
}
Then inherit the _prepare_invoice function in sale_order. In this function you are going to pass the sale order id as source id to the account.invoice
class sale_order(osv.osv):
_inherit = "sale.order"
def _prepare_invoice(self, cr, uid, order, lines, context=None):
if context is None:
context = {}
journal_id = self.pool['account.invoice'].default_get(cr, uid, ['journal_id'], context=context)['journal_id']
if not journal_id:
raise osv.except_osv(_('Error!'),
_('Please define sales journal for this company: "%s" (id:%d).') % (order.company_id.name, order.company_id.id))
invoice_vals = {
'name': order.client_order_ref or '',
'origin': order.name,
'type': 'out_invoice',
#Sale order id as source_id
'source_id':order.id,
'reference': order.client_order_ref or order.name,
'account_id': order.partner_invoice_id.property_account_receivable.id,
'partner_id': order.partner_invoice_id.id,
'journal_id': journal_id,
'invoice_line': [(6, 0, lines)],
'currency_id': order.pricelist_id.currency_id.id,
'comment': order.note,
'payment_term': order.payment_term and order.payment_term.id or False,
'fiscal_position': order.fiscal_position.id or order.partner_invoice_id.property_account_position.id,
'date_invoice': context.get('date_invoice', False),
'company_id': order.company_id.id,
'user_id': order.user_id and order.user_id.id or False,
'section_id' : order.section_id.id
}
invoice_vals.update(self._inv_get(cr, uid, order, context=context))
return invoice_vals
Add this in View file
<record id="invoice_form" model="ir.ui.view">
<field name="name">account.invoice.form</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='date_invoice']" position="after">
<field name="source_id"/>
</xpath>
</field>
</record>
Now add this in your report file
<div name="x_delivery_date" t-if="doc.x_delivery_date">
<strong>Delivery Date:</strong>
<p t-field="doc.x_delivery_date"/>
<p t-field="doc.source_id.commitment_date"/>
</div>

Force field value if created from another action

I have a model that inherits from "hr.employee" and I have an action that will
display only the employees which are in a special department.
I would like that the "Create" button on top of that list allows me to
create a record in hr.employee with the department field already set to that special department.
Of course the other "Create" button, from the standard "hr" module,
should not set a default value for the department.
Here's the model:
# -*- coding: utf-8 -*-
from openerp import models, fields
class Patient(models.Model):
_inherit = 'hr.employee'
date_debut_contrat = fields.Date('Début Contrat de Prise en Charge')
date_fin_contrat = fields.Date('Fin Contrat de Prise en Charge')
And here's the action:
<record id="action_liste_patients" model="ir.actions.act_window">
<field name="name">Patients</field>
<field name="res_model">hr.employee</field>
<field name="view_mode">kanban,tree,form</field>
<field name="domain">[('department_id.is_patients', '=', 'true')]</field>
</record>
Do I have a way to tell Odoo that from that action, the department_id field should have a default value, without changing the behaviour of the standard action ?
Many thanks to all.
Marc
You can use
<field name="context">{'department_id': 'special department'}</field>
Now while creating Patient, in create() you can fetch value from context and act accordingly.
Based on Jamin's comment, here's the full solution I applied:
1) Add a context to the action. The context will have a value named
"create_as_patient", which is True or False.
<record id="action_liste_patients" model="ir.actions.act_window">
<field name="name">Patients</field>
<field name="res_model">hr.employee</field>
<field name="view_mode">kanban,tree,form</field>
<field name="domain">[('department_id.is_patients', '=', 'true')]</field>
<field name="context">{'create_as_patient': True}</field>
</record>
2) In the create() method of my model, I fetch the value from the
context if it's present. Then I search in the "hr.department" model
for the department which is marked as default department for patients.
Then, if the default department has been found, I set the new record's
department_id to it.
# -*- coding: utf-8 -*-
from openerp import models, fields, api
class Patient(models.Model):
_inherit = 'hr.employee'
date_debut_contrat = fields.Date('Début Contrat de Prise en Charge')
date_fin_contrat = fields.Date('Fin Contrat de Prise en Charge')
#api.model
def create(self, vals):
if 'create_as_patient' in self.env.context:
if self.env.context['create_as_patient']:
departmentId = self.env['hr.department'].search([('is_patients', '=', True)], limit=1).id
if departmentId is not None:
vals['department_id'] = departmentId
record = super(Patient, self).create(vals)
return record
This meets my requirements :
From the Employee action, which doesn't have the 'create_as_patient'
flag in its context, newly created employees will not have the
default department set.
On the other hand, if I create a record from
my Patient action, the flag will be present, and the default
department will be added to the new record.
Many thanks again to jamin for pointing me the right direction :-)
Marc

How can I filter products in a sales orders line using a value linked to a variant attribute value of a product?

I'm using Odoo 8 and I have added the “origen” attribute to the sale_order class, as I mentioned above this field is linked using a many2one relation to a product attribute value.
This is the class definition:
class sale_order(osv.osv):
_name = "sale.order"
_inherit = 'sale.order'
_columns = {
'fecha_entrega_cliente': fields.datetime('Entrega Solicitada'),
'turno_id': fields.many2one('calendar.event','Turno'),
'obra_id': fields.many2one('sale.obra','Obra', domain="[('name','=',partner_id)]"),
'ubicacion': fields.char('Ubicacion'),
'zona': fields.many2one('sale.zona' ,string='Zona'),
'origen': fields.many2one('product.attribute.value' ,string='Origen'),
}
I want to filter the products shown in a sales order line using the 'origen' attribute. I tried using .xml:
<xpath expr="//tree[#string='Sales Order Lines']/field[#name='product_id']" position="attributes">
<attribute name="domain">[('attribute_line_ids', '=', origen)]</attribute>
</xpath>
But it doesn't work…
Also tried this in the .py:
class sale_order_line(osv.osv):
_inherit="sale.order.line"
_name="sale.order.line"
_columns={
'origen':fields.related('order_id','origen','id',type="integer"
,string="Origen"),
'product_id': fields.many2one('product.product', 'Product', domain=[('sale_ok', '=', True),('product_variant_ids.attribute_value_ids.attribute_id.id', '=', 'origen')], change_default=True, readonly=True, states={'draft': [('readonly', False)]}, ondelete='restrict'),
However, it doesn't show any product.