Odoo-15. How to disable record deletion (bool field)? - python-3.x

I made all the other entries false if one entry is true. How do I protect this entry from being deleted?
There must always be at least one entry.
res.partner.xml
<odoo>
<record id="view_partner_form_inherit" model="ir.ui.view">
<field name="name">res.partner.form</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//form/sheet/group/group/field[#name='mobile']" position="after">
<field name="is_primary" string='Is primary'/>
</xpath>
<div t-att-class="color + (record.title.raw_value == 1 ? ' oe_kanban_color_alert' : '') + ' oe_kanban_global_click'">
<div style="float: right; margin-top:-25%;">
<xpath expr="//div[hasclass('oe_kanban_details')]" position="inside">
<field name="is_primary" widget="checkbox"/>
</xpath>
</div>
</div>
</field>
</record>
</odoo>
Primary_contact.py
from odoo import api, fields, models, _
class PartnerPrimary(models.Model):
_inherit = "res.partner"
is_primary = fields.Boolean(string='Is primary')
#api.constrains('is_primary')
def check_is_primary(self):
if self.is_primary:
contacts_ids = self.parent_id.child_ids.filtered(lambda lm: lm.id != self.id)
for cont in contacts_ids:
cont.is_primary = False

I solved this problem
class PartnerPrimary(models.Model):
_inherit = "res.partner"
is_primary = fields.Boolean(string='Is primary')
#api.constrains('is_primary')
def check_is_primary(self):
if self.is_primary:
contacts_ids = self.parent_id.child_ids.filtered(lambda lm: lm.id != self.id)
for cont in contacts_ids:
cont.is_primary = False
def unlink(self):
for delete in self:
if delete.is_primary:
raise UserError(('You cannot delete'))
return super(PartnerPrimary, self).unlink()

Related

reading a field value from another model odoo13

what i'm trying to do here is creating a new field dimension that gets value from a field in the [stock.move] model this approach doesn't work i don't know why and it's not getting any errors from the python file the only error from the xpath tag at the xml file
this is models.py file
xx_Custom_Invoice(models.Model):
_inherit = 'account.move'
dimension = fields.Char(String="XxX", compute='get_data')
def get_data(self):
stock_move = self.env['stock.move']
for move in self:
picking = stock_move.picking_id
if move.id != picking.account_move_ids:
continue
for line in move:
line.update({
'dimension': line.dimension,
})
here is the views.xml
<record id="xx_account_move_view_inherit" model="ir.ui.view" >
<field name="name">account.move.view.inherit</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<xpath expr="/form/sheet/notebook/page/field[#name='invoice_line_ids']/tree/field[#name='product_id']" position="after">
<field name="dimension"/>
</xpath>
</field>
</record>
There is an issue here:
stock_move = self.env['stock.move']
for move in self:
stock_move here is an empty recordset, so no iteration will happen in the for loop.
You probably to use search() to fetch the record you want to operate on:
stock_move = self.env['stock.move'].search(<DOMAIN>)
for move in self:

How to display fields from parent model at my custom module view?

myview.xml
<odoo>
<data>
<record model = "ir.ui.view" id = "custom_expense.list">
<field name = "name"> custom_expense list </field>
<field name = "model"> custom_expense.custom_expense </field>
<field name = "arch" type = "xml">
<tree>
<field name = "name" />
<field name = "test" />
</tree>
</field>
</record>
<record model = "ir.actions.act_window" id = "custom_expense.action_window">
<field name = "name"> custom_expense </field>
<field name = "res_model"> custom_expense.custom_expense </field>
<field name = "view_mode"> tree, form </field>
</record>
<menuitem name = "custom_expense" id = "custom_expense.menu_root" />
<menuitem name = "Menu 1" id = "custom_expense.menu_1" parent = "custom_expense.menu_root" />
<menuitem name = "Submenu 1" id = "custom_expense.menu_1_list" parent = "custom_expense.menu_1"
action = "custom_expense.action_window" />
</data>
</odoo>
mymodel.py
from odoo import models, fields, api
class custom_expense (models.Model):
_name = 'custom_expense.custom_expense'
_inherit = 'hr.expense'
test = fields.Char (string = 'Valuethird')
I would like to display "name" field from hr.expense model but I found error: TypeError: Many2many fields custom_expense.custom_expense.tax_ids and hr.expense.tax_ids use the same table and columns - - -
I think you are trying to add a new field to hr.expense. If thats the case you dont need to create new model. only the _inherit will be enough and no need of _name in python file and just inherit the xml file. If you want the new table, then you will need to redefine the many2many in the parent model.

enable only logged in user to view working hours field [duplicate]

I want to view 'working_hours' field only for employee, his manager and 'hr.group_hr_user' group.
how to hide this field automatically without edit form or trigger a button
class InheritHrEmployee(models.Model):
_inherit = 'hr.employee'
def hide_working_hours(self):
if self.env.uid == self.user_id.id or self.env.uid == self.parent_id.user_id.id or self.env.user.has_group(
'hr.group_hr_user'):
self.working_hours_view = True
else:
self.working_hours_view = False
working_hours_view = fields.Boolean(computed=hide_working_hours)
XML:
<record id="hide_working_hours_for_employees" model="ir.ui.view">
<field name="name">Hide Working Hours Employees Form</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='resource_calendar_id']" position="before">
<field name="working_hours_view" invisible="1"/>
</xpath>
<xpath expr="//field[#name='resource_calendar_id']" position="attributes">
<attribute name="attrs">{'invisible': [('working_hours_view' ,'=', False)]}</attribute>
</xpath>
</field>
</record>
Try below code for display working hours field only hr.group_hr_user group users.
XML:
<record id="hide_working_hours_for_employees" model="ir.ui.view">
<field name="name">Hide Working Hours Employees Form</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='resource_calendar_id']" position="before">
<field name="working_hours_view" invisible="1"/>
</xpath>
<xpath expr="//field[#name='resource_calendar_id']" position="attributes">
<attribute name="groups">hr.group_hr_user</attribute>
</xpath>
</field>
</record>
You can add multiple attributes in the XML file like above code.
i added the field before the function and it works now automatically
class InheritHrEmployee(models.Model):
_inherit = 'hr.employee'
inv = fields.Boolean(string="Invisible", compute="c_inv", store=False)
#api.one
def c_inv(self):
if self.env.uid == self.user_id.id or self.env.uid == self.parent_id.user_id.id or self.env.user.has_group(
'hr.group_hr_user'):
self.inv = False
else:
self.inv = True
.. like this example
make fields visible to user and invisible to other

Compute boolean field with store=True is not search in Odoo11

For below code my boolean field not domain(searched) in database. It's work when i make this method depends. But i have above 1000 records so how to domain in action for all record simultaneously?
**Python Code:**
#api.multi
def _compute_opportunity_count111(self):
value = {}
for rec in self:
operator = 'child_of' if rec.is_company else '=' # the opportunity count should counts the opportunities of this company and all its contacts
won_list = rec.env['crm.lead'].search(
[('partner_id', operator, rec.id), ('stage_id.probability', '=', 100)]).ids
if won_list:
rec.won_customer = True
value.update(won_customer=rec.won_customer)
won_customer = fields.Boolean(compute='_compute_opportunity_count111', store=True)
**XML Code:**
<record id="base.action_partner_form" model="ir.actions.act_window">
<field name="name">Customers</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">res.partner</field>
<field name="view_type">form</field>
<field name="view_mode">kanban,tree,form</field>
<field name="context">{"search_default_customer":1}</field>
<field name="domain">[('won_customer', '=', True)]</field>
<field name="search_view_id" ref="base.view_res_partner_filter"/>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click to add a contact in your address book.
</p>
<p>
Odoo helps you easily track all activities related to
a customer: discussions, history of business opportunities,
documents, etc.
</p>
</field>
</record>
Thanks in advance
You should not use api.multi but api.depends -> this makes sure the method is called again when relevant fields changed.
Seems you depend on is_company -> #api.depends('is_company')
Also, from what I read your value dict is useless -> you can remove it

Odoo10 ProgrammingError: column id does not exist in pivot-table

When clicking menu for open the pivot view, I keep encountering the error "ProgrammingError: column new_table_report.id does not exist".
My code file .py:
#api.model_cr
def init(self):
tools.drop_view_if_exists(self._cr, 'new_table_report')
self._cr.execute("""
CREATE or REPLACE VIEW new_table_report AS (
SELECT sub.*,
(SELECT name FROM maintenance_rating_score_range WHERE
(sub.average_deviation >= from_score and
sub.average_deviation <= to_score)
) as rating
FROM(
SELECT
eq.id as equipment_id,
eq.name as name,
eq.category_id as category_id,
eq.partner_id as partner_id,
count(m.id) as number_service,
(sum(rs.score)/count(m.id)) as average_deviation
FROM maintenance_request m
INNER JOIN maintenance_equipment eq on (m.equipment_id = eq.id)
INNER JOIN maintenance_rating r on (r.request_id = m.id)
LEFT JOIN maintenance_rating_score rs on (r.rating_satisf_id = rs.id)
GROUP BY eq.id, eq.partner_id
)AS sub
)
""")
My code file .xml:
<record model="ir.ui.view" id="view_new_table_report_pivot">
<field name="name">new.table.report.pivot</field>
<field name="model">new.table.report</field>
<field name="arch" type="xml">
<pivot string="Report Analysis" disable_linking="False">
<field name="equipment_id" type="row"/>
<field name="number_service" type="measure"/>
<field name="average_deviation" type="measure"/>
</pivot>
</field>
</record>
<record model="ir.actions.act_window" id="action_new_tabke_report">
<field name="name">Report Analysis</field>
<field name="res_model">new.table.report</field>
<field name="view_type">form</field>
<field name="view_mode">pivot,graph</field>
<field name="context">{}</field>
</record>
Thank you for Advance.
I can do it, i don't create id field in new table report

Resources