can a many2many field in openerp/odoo be searchable? - odoo

I've added a many2many field in res.partner:
class res_partner(osv.osv):
_inherit = "res.partner"
_columns = {
'available_in': fields.many2many('res.country.state', 'copertura_country_state_rel', 'partner_id', 'country_state_id',),
}
And I now want to be able to search in partners list for "partners available in this country.state", but it seems the field can't be made searchable in a simple way (or I am missing something ?)

Related

Odoo 13 : how to allow field to be editable in form builder

I want to allow field to be editable in form builder,
I added "title" field in a custom odoo form (contact form), but it shouldn't in website_form_blacklised, so I wrote this code :
class CrmLead(models.Model):
_inherit = "crm.lead"
title = fields.Many2one(website_form_blacklisted=False)
But it doesn't work,
Can you help me?
Solved, I added this in xml,
<function model="ir.model.fields" name="formbuilder_whitelist">
<value>crm.lead</value>
<value eval="[
'title'
]"/>
</function>

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.

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)

How can I make a field readonly from fields_view_get in OpenERP7?

What I want:
Based on two context variables which must BOTH exist (one default value, and a flag), I must make a field readonly. If either of the context variables is not present, the field should be editable as usual.
I have this file:
from osv import fields, osv
from lxml import etree
class ir_sequence(osv.osv):
_name = 'ir.sequence'
_inherit = 'ir.sequence'
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
"""
We set the field to readonly depending on the passed flags. This means:
* We must specify to fix the sequence type (fixed_sequence_type=True).
* We must specify a default value for the "code" (default_code=my.custom.seq.code).
This only applies to context (e.g. a context node in a ir.action.act_window object, or a <field /> tag for a
relational field to this object, with a context with these values).
"""
res = super(ir_sequence, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type,
context=context, toolbar=toolbar, submenu=submenu)
context = context or {}
is_fixed = context.get('fixed_sequence_type', False) and bool(context.get('default_code', False))
if is_fixed and 'code' in res['fields']:
res['fields']['code']['readonly'] = 1
#arch = etree.XML(res['arch'])
#for node in arch.xpath("//field[#name='code']"):
# node.set('readonly', '1')
#res['arch'] = etree.tostring(arch)
return res
ir_sequence()
And I tried two alternatives to change the readonly attribute of a field to True when a condition was met (condition is given by the is_fixed variable - by debugging I see that it gets the True value it needs, when I trigger it in the intended manner).
The first alternative was edit the arch content as XML, find the node for the field 'code', and fix it. The code for that alternative is commented.
The second alternative was edit the fields dictionary, find the 'code' field, and set readonly=True on that field.
Neither of them worked (symptoms: the field is not readonly when the condition evaluates to True).
What must I do to make it work?
Try this,
<field name="field_name" invisible="context.get('flag1',False) and context.get('flag2',False)" />
you can pass context to list view using action which is bind to the list view.
<record id="action_account_tree1" model="ir.actions.act_window">
<field name="name">Analytic Items</field>
<field name="res_model">account.analytic.line</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="context">{'flag1':True,'flag2':True}</field>
</record>
You have to manage logical conditions as per your need.
No need to override fields_view_get if your aim is to hide fields from list view based on context then.
Thanks.

One Module fields use in other module

I am trying to use some fields of "Job Positions" in "Opportunities" and we can say it as a merger fields. but i am unable to do this task . i have no knowledge about Python language.
I have created a user defined fields and use it in opportunities by XML coding by developer options . I know that it is easy because user defined fields has "crm.lead" named module which is same in opportunities.
but now When i want to use this fields in "hr.job" then it give me error "fields not found" . i know that this fields is not in current module and it is part of "crm.lead" not "hr.job".
Is it possible to use one module fields in another module?
Yes you can do so. First you have to create an object for that and then browse the record and fetch the value of the field which you want.
For example create one method and then browse the crm.lead record:
crm_obj = self.pool.get('crm.lead')
crm_brw = crm_obj.browse(cr, uid, crm_rec_id, context=context)
print "my field value:: ", crm_brw.your_field
Here, "crm_rec_id" is the id of the record of crm.lead object
There are lots of examples given in addons.
yes you can it done by _inherits.
for eg.
hr_job in hr module .
class hr_job(osv.osv):
_name = "hr.job"
_description = "job position"
_columns = {
'name': fields.char('job name', size=64)
}
crm_lead in crm module.
class crm_lead(osv.osv):
_name = "crm.lead"
_inherits = {'hr.job': 'job_id'}
_description = "Lead/Opportunity"
_columns = {
'partner_id': fields.many2one('res.partner', 'Partner')
}
in xml file of crm create form view.
<record id="crm_lead_form" model="ir.ui.view">
<field name="name">crm.lead</field>
<field name="model">crm.lead</field>
<field name="arch" type="xml">
<form>
<field name="name"/> # job name from hr_job
<field name="partner_id"/> # partner_id from crm.lead
</form>
</field>
</record>
don't forget add dependency.