How to use Contacts (partnet) address fields in custom model in Odoo - odoo

In the contact application we can see that every contact (partner model) has an address. That address if form by several fields (Street, Street 2, city, state, zip, country) How can a add those fields to a custom model. I net the code for both the model and the view
This is what I want for my custom model

new_model_name/models/res_partner.py
from odoo import models, fields, api
class ResPartner(models.Model):
_inherit = 'res.partner'
new_field = fields.Char(string='Full Address', compute='_compute_full_address', store=True)
#api.depends('street', 'street2', 'city', 'state_id', 'country_id')
def _compute_full_address(self)
for r in self:
r.new_field = f'{r.street}, {r.street2}, {r.city}, {r.state_id.name}, {r.country_id.name}'
new_model_name/views/res_partner_views.xml
<odoo>
<record id="res_partner_view_form" model="ir.ui.view">
<field name="name">res.partner.view.form</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<xpath expr="//div[hasclass('o_address_format')]" position="inside">
<field name="new_field" />
</xpath>
</field>
</record>
</odoo>

Related

How to do Prototpye inherit in odoo

i suppose I've not fully undrestood the prototype inheritance in Odoo.
I try to inherit crm.lead
The Model :
` class learn_odoo(models.Model):
_name = 'learn_odoo.learn_odoo'
_inherit = ['crm.lead']
_description = 'learn_odoo.learn_odoo'
tag_ids = fields.Many2many('mail.channel','mail_channel_profile_crm', 'partner_id', 'tag_id')
job = fields.Char()`
The View :
`<record id="view_inherit_list_crm" model="ir.ui.view">
<field name="name">Learn Odoo</field>
<field name="model">learn_odoo.learn_odoo</field>
<field eval="1" name="priority"/>
<field name="inherit_id" ref="crm.crm_case_tree_view_oppor"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='name']" position="after">
<field name="job"/>
</xpath>
</field>
</record>
<record id="learn_odoo.action_window" model="ir.actions.act_window">
<field name="name">Learn Odoo</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">learn_odoo.learn_odoo</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
</p>
<p>
</p>
</field>
</record>`
The problem I encountered :
From your question, what i understood is you are trying to inherit crm.lead if i am right then please remove the _name = 'learn_odoo.learn_odoo'
you have to do as i am doing below:
class learn_odoo(models.Model):
_inherit = 'crm.lead'
tag_ids = fields.Many2many('mail.channel','mail_channel_profile_crm', 'partner_id', 'tag_id', string='Tag')
job = fields.Char('Job')
also in __manifest__.py file add crm in depends.
If you are not clear about inheritance please go through the odoo docs for inheritance from this Link for inheritance select your odoo version from top right corner for better results.

In inherited search view field not found

First I Inherit hr.employee model in my custom module and add a field unique_id. Than in my custom module I inherit hr.employee search view for this unique_id field. But throw this error, Field "unique_id " does not exist in model "hr.employee"
Here is python file:
class HrUniqueId(models.Model):
_inherit = "hr.employee"
unique_id = fields.Char(string='ID', required=True, copy=False, readonly=True,
default=lambda self: _('New'))
Here is XMLfile:
<record model="ir.ui.view" id="hr_employee_search_inherit">
<field name="name">hr.employee.search.inherit</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_filter"/>
<field name="arch" type="xml">
<xpath expr="//search" position="inside">
<field name="unique_id" string="Employee Id"/>
</xpath>
</field>
</record>
Error massage is:
<search string="Employees">
<field name="name" string="Employee" filter_domain="['|', ('work_email', 'ilike', self), ('name', 'ilike', self)]"/>
<field name="category_ids" groups="hr.group_hr_user"/>
<field name="job_id"/>
<separator/>
Field "unique_id" does not exist in model "hr.employee"
View error context:
{'file': 'c:\\odoo\\fornew\\fornew\\odoo\\custom\\employee_details\\views\\employee_details.xml',
'line': 3,
'name': 'hr.employee.search.inherit',
'view': ir.ui.view(2023,),
'view.model': 'hr.employee',
'view.parent': ir.ui.view(366,),
'xmlid': 'hr_employee_search_inherit'}

Odoo 11 - pull fields values from other models

I have added a custom field (many2one) in crm.lead form and a custom class for that
Here is my code (I am using odoo 11):
models.py
from odoo import models, fields, api
# Add fields to Leads
class Leads_events(models.Model):
_inherit = 'crm.lead'
venue = fields.Many2one('crm.lead.venue',String="Venue")
venue_city = fields.Char(String="City")
venue_country = fields.Many2one('res.country', string="Country")
# Create venue class
class Leads_venue(models.Model):
_name = 'crm.lead.venue'
name = fields.Char(string="Venue name")
city = fields.Char(string="City")
country = fields.Many2one('res.country', string="Country")
address = fields.Text(string="Address")
website = fields.Char(string="Website")
phone = fields.Char(string="Phone")
fax = fields.Char(string="Fax")
notes = fields.Text(string="Notes")
views.xml
<record id="crm_lead_venue_form" model="ir.ui.view">
<field name="name">crm.lead.venue.form</field>
<field name="model">crm.lead.venue</field>
<field name="arch" type="xml">
<form string="Venues">
<group>
<group>
<field name="name"/>
<field name="country"/>
<field name="city"/>
<field name="address"/>
</group>
<group>
<field name="phone"/>
<field name="fax"/>
<field name="website"/>
</group>
<group>
<field name="notes" />
</group>
</group>
</form>
</field>
</record>
<record id="crm.crm_case_form_view_oppor_events" model="ir.ui.view">
<field name="name">crm_case_form_view_oppor_events</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_case_form_view_oppor"/>
<field name="arch" type="xml">
<xpath expr="//notebook" position="before">
<group string="Venue">
<field name="venue"/>
<field name="event_city" string="City"/>
<field name="event_country"/>
</group>
</xpath>
</field>
</record>
This works fine. Now what I am trying to pull is the city and country from crm.lead.venue selected record to crm.lead 'venue_city' and 'venue_country' fields.
I looked at onchange function but I cannot find how to make it work...
I've tried this following another post I read but it is not working
#api.onchange('venue')
def onchange_venue(self):
if self.venue:
self.event_city = self.venue.city
self.event_country = self.venue.country
Is there anywhere I can find a documentation or help more complete on this ? The official documentation is not very precise on this matter.
The onchange method works when the user change the field in the view, and I cant see the field on your view, so the method would be never be called. You should add "venue" field to the form and test it again.
Edit
Have you seen that it is not event_city but venue_city? The onchange should be on leads_events. Did you test that onchange is executed and has no value? Or it doesnt even execute the function ?
#api.onchange('venue')
def onchange_venue(self):
if self.venue:
self.venue_city = self.venue.city
self.venue_country = self.venue.country

Domain for on field based on another

In task templates form i can add group_id. I want to make a domain that will add task templates depending in what group they belong but kinda have no clue now.
class ProjectTaskGroup(models.Model):
_name = 'project.task.group'
_inherit = 'project.object'
name = fields.Char(string="Name", required=True)
class ProjectTaskTemplate(models.Model):
_name = 'project.task.template'
_inherit = 'project.object'
name = fields.Char(string="Name", required=True)
group_id = fields.Many2one('project.task.group', string="Task Group")
<!-- Project Task Views -->
<record id="view_task_form2" model="ir.ui.view">
<field name="name">project.task.form</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_form2"/>
<field name="arch" type="xml">
<xpath expr="//div[#class='oe_title']" position="before">
<div class="oe_inline oe_edit_only">
<field name="group_id" class="oe_inline"/>
<field name="task_template_id" class="oe_inline"/>
</div>
</xpath>
</field>
</record>
First add field in model 'project.task.group'
template_id = fields.One2many('project.task.template', 'group_id', string='Group task')
Field of 'project.task'
task_template_id = field.Many2one('project.task.template')
group_id=field.Many2one('project.task.group')
In view of 'project.task'
<field name="task_template_id" class="oe_inline"/>
<field name="group_id" class="oe_inline" domain="[('template_id', '=', task_template_id)]"/>
first select template so we get group_id belogs to task_template_id

Add customer field in openerp pos module

I am using openerp 6.1.Here in pos module there is no feature of selecting customer.I want to add this field.But i couldn't.Can anyone help me???
You can add extra fields by inheriting any model in your customized module.
You have to inherit the "pos.order" object & view.
Inherit 'pos.order' in your .py:
class pos_order(osv.osv):
_inherit = 'pos.order'
_columns = {
'customer_id': fields.many2one('res.partner', 'Customer'),
}
pos_order()
Above code will create a field in your database.
Now, To display your field in your form, inherit the view of 'pos.order' in your .xml,:
<record id="inherited_form_pos_view" model="ir.ui.view">
<field name="name">pos.order.form.inherit</field>
<field name="model">pos.order</field>
<field name="type">form</field>
<field name="inherit_id" ref="point_of_sale.view_pos_pos_form"/>
<field name="arch" type="xml">
<field name="name" position="after">
<field name="customer_id"/>
</field>
</field>
</record>
Now, you can see "Customer" field in your pos.order form.