add checkbox field on product page on odoo - odoo

i added a checkbox on product page
When (checkbox_customtext) field is TRUE a (customtext) field should appear on the site
When an False field should disappear
I wrote the code but does not work
Please help me solve the problem
py xml files
<odoo>
<template id="website_insert_customtext" inherit_id="website_sale.address">
<xpath expr="//div[#id='div_phone']" position="after">
<!--hier is not working-->
<figure t-foreach="product_template" t-as="o" t-if="o.checkbox_customtext == True" class="snip1533">
<div t-attf-class="form-group #{error.get('customtext') and 'has-error' or ''} col-md-6"
id="div_custom_text">
<label class="control-label" for="customtext">Custom Text ( Only For Tactical vest )</label>
<input name="customtext" class="form-control"
t-att-value="'customtext' in checkout and checkout['customtext']"/>
</div>
</figure>
</xpath>
</template>
by .py
from odoo import models, fields
class ProductTemplate(models.Model):
_inherit = 'product.template'
checkbox_customtext = fields.Boolean('Add Custom field', default=True, )
class ResPartner(models.Model):
_inherit = 'sale.order'
customtext = fields.Char('Custom Text')

You can do it without using template. Just add to your customtext field in xml attrs="{'invisible': #your_domain}" and it will do the job.
<field name="customtext" attrs="{'invisible':[('checkbox_customtext', '=', False)]}"/>

Related

How do I migrate a module to remove a custom `res.config.settings` field?

Suppose I have a module which defines some custom res.config.settings fields like so:
from odoo import models, fields
class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"
custom_field = fields.Char(config_parameter="custom.field")
some_other_custom_field = fields.Char(config_parameter="some.other.custom.field")
And then in my XML I have something like this:
<odoo>
<record id="custom_settings_view_form" model="ir.ui.view">
<field name="name">custom.settings.view.form</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="custom_base_settings_view_form"/>
<field name="arch" type="xml">
<div data-string="custom" position="inside">
<h2>Custom</h2>
<div class="row mt16 o_settings_container">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="custom_field"/>
<div class="text-muted">Description...</div>
<div class="content-group">
<div class="mt16">
<field name="custom_field"
placeholder="Placeholder..."/>
</div>
</div>
</div>
<div class="o_setting_right_pane">
<label for="some_other_custom_field"/>
<div class="text-muted">Description...</div>
<div class="content-group">
<div class="mt16">
<field name="some_other_custom_field"
placeholder="Placeholder..."/>
</div>
</div>
</div>
</div>
</div>
</div>
</field>
</record>
</odoo>
Suppose that now later I've implemented some new functionality elsewhere that eliminates my need for custom_field. As such, I'd like to remove it entirely. However, as you can see, the custom_settings_view_form also includes some_other_custom_field, which I need to keep. This means that my options are limited:
I cannot just remove the ir.ui.view record, because then I would lose the UI for some_other_custom_field.
I also cannot just uninstall and then install the module again, because it would result in data loss elsewhere.
So specifically I want to remove the following line from my model:
custom_field = fields.Char(config_parameter="custom.field")
And the following block from my XML:
<div class="o_setting_right_pane">
<label for="custom_field"/>
<div class="text-muted">Description...</div>
<div class="content-group">
<div class="mt16">
<field name="custom_field"
placeholder="Placeholder..."/>
</div>
</div>
</div>
However, when I remove that code and then run my Odoo server again with -u <name_of_containing_module>, I get the following error:
odoo.tools.convert.ParseError: "Error while validating view
Field `custom_field` does not exist
Error context:
View `custom.settings.view.form`
[view_id: <id>, xml_id: <name_of_containing_module>.custom_settings_view_form, model: res.config.settings, parent_id: <parent_id>]
...
I just learned that I can create scripts at <module_root>/migrations/<version>/ to help with this sort of situation, but I have no idea what I would need to put in my migration script to deal with this issue. How can I remove fields and modify views in a module without causing errors and without requiring that the module be reinstalled?
You could try this steps:
Remove custom_field from XML view and change the actual id
custom_settings_view_form.
Update your module, that shouldn't produce any error and you won't see custom_field any more.
At this moment you could deside ignore custom_field, nothing will happend. Any way if you are a person that don't like death code inside his project like me, you can continue with the next steps.
Remove custom_field definition for .py file.
Update your module.
If you have the same error reported, you sould execute the 3.1 step bellow.
3.1. Clean the database data for custom_field. By SQL. Continue with 4 and 5 steps.
Please share your comments of the execution of these steps.
I hope this answer can be helpful for you.

Settings page can save values but does not display saved values

I am trying to create a custom settings page using res.config.settings
I'm using res_config_settings_views.xml in the hr addons folder as a reference for the view.
And I'm using res_config_settings.py also in the hr addons folder as a reference for the model.
It worked I can see my custom settings page. I can save the values. But after I saved, the settings page back to its unset state.
When I look at the database, the values were saved. And I can call the value to use in my model.
Just confused why the settings page cannot display the values I saved?
Am I missing something?
# -*- coding: utf-8 -*-
from odoo import fields, models
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
president_director_id = fields.Many2one(comodel_name='hr.employee', string="President Director", readonly=False)
head_of_hr_id = fields.Many2one(comodel_name='hr.employee', string="Head of Human Resources", readonly=False)
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="name">res.config.settings.view.form.inherit.fhid_recruitment</field>
<field name="model">res.config.settings</field>
<field name="priority" eval="65"/>
<field name="inherit_id" ref="base.res_config_settings_view_form"/>
<field name="arch" type="xml">
<xpath expr="//div[hasclass('settings')]" position="inside">
<div class="app_settings_block" data-string="FHID Settings" string="FHID Settings" data-key="fhid_setting">
<h2>FHID Settings</h2>
<div class="row mt16 o_settings_container">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="president_director_id"/>
<!-- <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/> -->
<div class="row">
<div class="text-muted col-lg-8">
Set default director, ...
</div>
</div>
<div class="content-group">
<div class="mt16">
<field name="president_director_id" class="o_light_label"/>
</div>
</div>
</div>
</div>
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="head_of_hr_id"/>
<!-- <span class="fa fa-lg fa-building-o" title="Values set here are company-specific." role="img" aria-label="Values set here are company-specific." groups="base.group_multi_company"/> -->
<div class="row">
<div class="text-muted col-lg-8">
Set default manager, ...
</div>
</div>
<div class="content-group">
<div class="mt16">
<field name="head_of_hr_id"/>
</div>
</div>
</div>
</div>
</div>
</div>
</xpath>
</field>
</record>
<record id="fhid_recruitment_config_settings_action" model="ir.actions.act_window">
<field name="name">Settings Manager</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">res.config.settings</field>
<field name="view_mode">form</field>
<field name="target">inline</field>
<field name="context">{'module' : 'fhid_recruitment'}</field>
</record>
<menuitem id="fhid_recruitment_menu_configuration"
name="Settings"
parent="hr.menu_human_resources_configuration"
sequence="0"
action="fhid_recruitment_config_settings_action"
groups="base.group_system"/>
</odoo>
You will have to use set_values method to save the field values and then to get the value again you will have to you get_values method.
For example you have the following field in res.config.settings model:
head_of_hr_id = fields.Many2one('hr.employee', string="Head")
Now in order to save this field value after clicking the Save button you will have to use the following code:
def set_values(self):
super(ResConfigSettings, self).set_values()
set_param = self.env['ir.config_parameter'].sudo().set_param
set_param('module_name.head_of_hr_id', int(self.head_of_hr_id.id))
To get the value again in the field, use the following code:
#api.model
def get_values(self):
res = super(ResConfigSettings, self).get_values()
get_param = self.env['ir.config_parameter'].sudo().get_param
res['head_of_hr_id'] = int(get_param('module_name.head_of_hr_id'))
return res
Hope this code help you.

How to disable clear button in attachment form view odoo11?

I am try to disable the clear button in attachment form.
I think this is js code. I search some js code in but nothing find.
Any hint for this problem??
This is the image of clear button
Another approach for only extending this one form view is to set the field dynamically to readonly when filled.
<record id="view_attachment_form" model="ir.ui.view">
<field name="name">disable remove button if filled</field>
<field name="model">ir.attachment</field>
<field name="inherit_id" ref="base.view_attachment_form" />
<field name="arch" type="xml">
<field name="datas" position="attributes">
<attribute name="attrs">{'invisible':[('type','=','url')], 'readonly':[('datas', '!=', False)]}</attribute>
</field>
</field>
</record>
And a part of the original view:
<sheet>
<label for="name" class="oe_edit_only"/>
<h1>
<field name="name"/>
</h1>
<group>
<group>
<field name="type"/>
<field name="datas" filename="datas_fname" attrs="{'invisible':[('type','=','url')]}"/>
<field name="datas_fname" invisible="1" attrs="{'invisible':[('type','=','url')]}" class="oe_inline oe_right"/>
<!-- and so on -->
If you want to remove the Button for the whole Binary field widget in Odoo, you could just "extend" the QWeb template for that widget, which is:
<t t-name="FieldBinaryFile">
<a t-if="widget.mode === 'readonly'" href="javascript:void(0)" class="o_form_uri"/>
<div t-if="widget.mode !== 'readonly'" class="o_field_binary_file">
<input type="text" class="o_input"
readonly="readonly"
t-att-name="widget.name"
t-att-tabindex="widget.attrs.tabindex"
t-att-autofocus="widget.attrs.autofocus"/>
<button type="button" class="btn btn-sm btn-primary o_select_file_button" title="Select">Upload your file</button>
<button type="button" class="btn btn-sm btn-default fa fa-pencil o_select_file_button" title="Select"/>
<button type="button" class="btn btn-sm btn-default fa fa-trash-o o_clear_file_button" title="Clear"/>
<span class="o_form_binary_progress">Uploading...</span>
<t t-call="HiddenInputFile">
<t t-set="fileupload_id" t-value="widget.fileupload_id"/>
<t t-set="fileupload_style" t-translation="off">overflow-x: hidden</t>
</t>
</div>
</t>
You can extend QWeb templates but have to load them in the manifest file under key qweb.
xml file normally in module at /static/src/xml
<templates>
<t t-name="web.FieldBinaryFile" t-extend="base.FieldBinaryFile">
<t t-jquery="button[title='Clear']"
t-operation="replace" />
</t>
</templates>
And the part of the manifest
{
'name': 'remove button in binary widget',
# and so on
'depends': [
'base',
],
'qweb': [
'static/src/xml/remove_button.xml'
],
# and so on
}

Generate html and render in qweb

Is it possible generate html in .py file and render in qweb?
<openerp>
<data>
<record id="paperformat_time" model="report.paperformat">
<field name="name">Time</field>
<field name="font_size">10</field>
</record>
<report id="time_qweb" model="hr_timesheet_sheet.sheet" string="Time"
report_type="qweb-pdf" name="time.report_time" file="time.report_time" />
<record id="time_qweb" model="ir.actions.report.xml">
<field name="paperformat_id" ref="time.paperformat_time" />
</record>
</data>
</openerp>
qweb
<template id="report_time">
<t t-call="report.html_container">
<t t-foreach="docs" t-as="t">
<span t-esc="t.__compute_html()" />
<div class="page">
<span t-field="t.html_text " />
</div>
</t>
</t>
</template>
.py file
class Time(models.Model):
_inherit = 'hr_timesheet_sheet.sheet'
html_text = fields.Html(string = 'Html')
#api.one
def _compute_html(self):
html_value = "<h1>TEST</h1>"
html_value += "<h1>TEST 2</h1>"
self.html_text = html_value
eg.
html_value = "<h1> + employee_id.name + "</h1>"
html_value += "<h1> + employee_id.phone + "</h1>"
now I need html_value render in qweb in put in <div class="page"> put here html_value </div>
Now I save text in database, any better solution?................................
Yes you can if you have a variable that have html code if you use t-esc or t-field odoo will print it as text.
If you want to render it use. t-raw
<div t-raw="doc.some_attribute" > </div>
Or
<t t-raw="doc.some_attribute" > </t>
You can try this
<span t-raw="my_html_field"/>
Here my_html_field is your html formated data
Since Odoo version 15, t-raw is deprecated. You need to render the HTML in a safe way using Python, then parse that into XML. Reference
import markupsafe
...
class YourController(http.Controller):
...
#http.route(...)
def your_rendering_method(self):
...
return request.render("YOUR_TEMPLATE", {"YOUR_FIELD": markupsafe.Markup("<a href='https://www.stackoverflow.com'>Stack Overflow</a>")})
<t t-esc="YOUR_FIELD" />

Reach field in invoice report

I need to extend invoice report with field note. but I'm getting error that field does not exist. I'm stuck and don't know how to reach that field. did tried with partner_id.note too but also getting error.
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<template id="report_invoice_document" inherit_id="account.report_invoice_document">
<xpath expr="//div[#class='page']/*[last()]" position="after">
<div class="row">
<div class="col-xs-6">
<span t-esc="o.note"/><br/>
<div class="left_sign_block">
<span>Note</span>
</div>
</div>
</div>
</xpath>
</template>
</data>
</openerp>
class AccountInvoiceTax(models.Model):
_inherit = 'account.invoice.tax'
note = fields.Text(related='tax_id.note', string='Note')
QWebException: "'account.invoice' object has no attribute 'note'" while evaluating 'o.note'
You have extended the model account.invoice.tax and not account.invoice. The error message is correct. So either you extend invoice or you have to use note from the invoice tax lines (tax_line_ids).