How to update a value from a field without refreshing the page Odoo - odoo-14

I'm creating a new sector on the settings page of my module, where I have a value and an update icon, which aims to update the field value when I click on the icon.
But when I call the function to execute the function, the page is reloaded and I never get the value, but the value is printed in the terminal with a logger, does anyone have any suggestions?
My XML code:
<button type="object" name="refresh_credits" class="btn-link" icon="fa-refresh"/>
<span class="btn-link">Credits</span>
<field name="new_credits"/>
My python code inside a class:
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
new_credits = fields.Integer()
def refresh_credits(self):
data_details_credits = self.env['show.credits'].content_credits_info()
_logger.info(self.env['show.credits'].content_credits_info()[4])
self.new_credits = data_details_credits[4]

You have to override set_values and get_values method to update the values in settings. We cannot directly update the values in general settings.
Below is the example:
def set_values(self):
super(ResConfigSettings, self).set_values()
self.env['ir.config_parameter'].sudo().set_param('MODULE_NAME.new_credits',
self.new_credits)
#api.model
def get_values(self):
res = super(ResConfigSettings, self).get_values()
config = self.env['ir.config_parameter'].sudo()
config_key = config.get_param('MODULE_NAME.new_credits')
if config_zoom_key:
res.update(
field_name=config_key
)
return res
And also for better understanding, watch this video:
Add fields general settings

Related

How to set dynamic domain for many2one field without onchange trigger?

I have a class in module
class CRMProjects(models.Model):
_name = 'crm.projects'
product_lines = fields.Many2many('crm.project.product.lines')
and a class in another module
class CoRG(models.Model):
_inherit = 'crm.lead'
project = fields.Many2one(comodel_name="crm.projects")
product_lines = fields.Many2one('crm.project.product.lines')
the first module has a button that opens new form from the second module and easily set dynamic domain on product_lines triggered by an onchange
domain = {'product_lines': [('id', 'in', self.project.product_lines.ids)]}
the problem is that if I go to the screen of records created from that form and edit the project_lines, the domain won't work
I tried to use
domain:lambda self:[('id', 'in', self.project.product_lines.ids)]
in the field of the 2nd module but this didn't work, any suggestions?
You do not need to set domain since the fields are related.
Change the as following:
class CoRG(models.Model):
_inherit = 'crm.lead'
project = fields.Many2one(comodel_name="crm.projects")
product_lines = fields.Many2one(related='project.product_lines')
Abdulrahman
You can set the default domain on many2one field like this,no need to trigger onchange
#api.model
def _default_tax_group(self):
return self.env['account.tax.group'].search([], limit=1)
tax_group_id = fields.Many2one('account.tax.group', string="Tax Group", default=_default_tax_group, required=True)
Thanks
Solved the issue using the following implementation
test1 = fields.Many2many('crm.project.product.lines', related='project.product_lines')
product_lines = fields.Many2one('crm.project.product.lines', domain="[('id', 'in', test1)]")
Thanks for your suggestions.

How to hide Edit button based on state in Odoo 11

how can I hide the edit button when the state is "In Progress"
I tried doing ir.rule like this but it didn't work it only filter(domain) my treeview
I also tried to doing it in JavaScript but i can't find any odoo 11 sample
This can be done by inserting conditional CSS.
Frist add a html field with sanitize option set to False:
x_css = fields.Html(
string='CSS',
sanitize=False,
compute='_compute_css',
store=False,
)
Then add a compute method with your own dependances and conditions:
# Modify the "depends"
#api.depends('state_str_modify_me')
def _compute_css(self):
for application in self:
# Modify below condition
if application.state_str_modify_me= 'In Progress':
application.x_css = '<style>.o_form_button_edit {display: none !important;}</style>'
else:
application.x_css = False
Finally add it to the view:
<field name="x_css" invisible="1"/>

how to disable a BUTTON after first click?

This might be a simple question.but,does any one know how to disable a button after clicking it in Odoo? Thanks for all your help.
Most of Odoo module use state for this kind of problem. the general idea of it that you must have a field and the button is displayed based on the value of that field.
Ex:
# in you model
bool_field = fields.Boolean('Same text', default=False)
In your view:
<button name="some_method"......... attrs="{'invisible': [('bool_field', '=', True)]}" />
...
...
...
<!-- keep the field invisible because i don't need the user to see
it, the value of this field is for technical purpuse -->
<field name="bool_field" invisible="1"/>
In your model:
#api.multi
def some_method(self):
# in this method i will make sure to change the value of the
# field to True so the button is not visible any more for user
self.bool_field = True
...
...
...
So if you all ready have a field that the button change it's value you can use it directly
or create a special field for this purpose.

Get url in body_html odoo 9

Is it possible get web page url in xml template?
<field name="body_html">
<![CDATA[
<p>Get url here</p>
]]>
</field>
Note: ${object.id}
return id eg. 10
In my opinion, QWeb has some fallbacks when it comes to doing what would otherwise be a simple thing in Python, such as accessing env to browse, search, or get other data like dbname or the company parameters like base_url.
What I've done in the past is just create a helper to have Python do the dirty work for you so that you can keep QWeb simple.
your_module/helpers/mixins.py
class CanGenerateUrl:
def generate_url(self):
"""
Build the URL to the record's form view.
- Base URL + Database Name + Record ID + Model Name
:param self: any Odoo record browse object (with access to env, _cr, and _model)
:return: string with url
"""
self.ensure_one()
base_url = self.env['ir.config_parameter'].get_param('web.base.url')
if base_url and base_url[-1:] != '/':
base_url += '/'
db = self._cr.dbname
return "{}web?db={}#id={}&view_type=form&model={}".format(base_url, db, self.id, self._model)
your_module/models/model.py
from openerp.addons.your_module.helpers.mixins import CanGenerateUrl
class YourModel(models.Model, CanGenerateUrl):
your_module/views/report.xml
<p><a href="${object.generate_url()}">${object.name or 'None'}<a/></p>
Reports Documentation
QWeb Documentation

Button in tree view odoo 9

I need a button in tree view for all line. After clicking on the button I need get line id.
I'm trying, but not working:
*.xml
<button name="copy_line" class="text-right" icon="fa-files-o" type="object"/>
*.py
#api.multi
def copy_line(self):
print("Not come here!")
for r in self:
print(r.id)
object has no attribute 'copy_line'
To call the method on button click that record should be saved.
But in this case, Record was not saved so you are not able to call the method on button click.
Alternet way is, you can create a new line based on onchange or button in the footer and add self._cr.commit() to commit and raise ValidationError.
You define copy_line in the wrong model.
If your button is included inside a tree view defined for a One2Many field line_ids and that field is referencing object.line, you method copy_line should be created in that model.
For example:
line_ids = fields.One2Many('object.line', 'ref_id', string='Lines')
class ObjectLine(models.Model):
_name = 'object.line'
#api.multi
def copy_line(self):
print("Not come here!")
for r in self:
print(r.id)