How to get data is on a remote server using odoo - odoo

On a remote server I have data in this form:
A project contains one or more tranches
A tranche contains one or more units
On my class odoo, I added these fields:
project_id: this is a many2one field to select the project. I can get back the list of project from the remote server
Tranche_id: A many2one field to select the tranche. I need when I change the project, this field should just list the tranches of the selected project.
Entity_id: A many2one field to select the Entity. I need when I change the tranche, this field should just list the entities of the selected tranche.
Is it possible to implement the many2one relation on Odoo with the constraint that the data is on a remote server and consumable REST API?
Help me please
Kindly.

Try this code:
#api.onchange('project_id')
def onchange_project_id(self):
res = {}
if self.object_id:
res['domain'] = {'tranche_id': [('project_id', '=', self.project_id.id)]}
return res
#api.onchange('tranche_id')
def onchange_tranche_id(self):
res = {}
if self.object_id:
res['domain'] = {'entity_id': [('tranche_id', '=', self.tranche_id.id)]}
return res
It may help in your case.

Related

How to apply an domain on relational field odoo?

Here is my sale.order.line module, I do some modifications
I want to apply a domain on the product that not all product be displayed just products in Ligne contract field for example :
Here in my Contrat line, I Have just one Product so on the Sale order lines only this product (article) must be shown
Use onchane event for this in your sale.order.line
#api.onchange('contrat_id')
def set_domain(self):
# force the user to reselect the producg if he changes the contrat line
self.product_id = False
if self.contrat_id :
return {'domain': {'product_id': [('id', 'in', self.contrat_id.product_ids.ids)]}}
else:
# remove the domain if no contrat is selected
return {'domain': {'product_id': []}}
I'm using my phone sorry if i made a syntax error but I hope you get the idea
Edits
Okay in your contract model you don't have a many2many field to product model as I thought instead you have this one2many field ligne contract
So let suppose that the name of that field is ligne_ids in this one2many relation there is a many2one field to product model let us say its name is product_id.
Use the power of mapped to extract in one line all product ids in the contract lignes.
# many2one -> one2many -> mapped('many2one') this will collect of the records without duplication from the o2m field.
# contract -> contract lignes -> products
self.contrat_lignes_id.ligne_ids.mapped('product_id').ids
Hope this helps you

How to send multiple values in many2many field in odoo?

I am trying to generate purchase order from manufacturing order.I have created many2many field for getting multiple products.I want to send multiple product ids to my custom function.I am able to send 1 value but sending more than one gives error as Expected singleton: product.template(4, 3).
from openerp import models,fields,api
class generate_purchase_order(models.Model):
_name = 'mrp_to_purchase_order'
product_id = fields.Many2many('product.template',string='Products',required=True)
#api.multi
def generate_purchase_order2(self):
for wizard in self:
mrp_obj = self.env['mrp.production']
mrp_obj.generate_purchase_order(wizard.product_id.id) #function call
return { 'type': 'ir.actions.act_window_close'}
mrp_custom.py,
from openerp import models,api
class mrp_production(models.Model):
_inherit = 'mrp.production'
#api.multi
def generate_purchase_order(self,product_id):
purchase_line_obj = self.env['purchase.order.line']
context = self._context
for order in self.browse(context['active_ids']):
for line in order.bom_id.bom_line_ids:
if line.product_id.id != product_id:#problem line
continue
#rest of code
singleton: product.template(4, 3)
This error means that code is expecting single record not recordset, so you must change code to allow recordset using or make ensureone with try-catch and avoid errors. Thats general information.
Now if you want to get multiple records from many2many its not problem at all, you must pass this many2many object only and then work with it.
After getting many2many object to work with every record from this recordset you must use for record in recordset:
Also wizard.product_id.id this is error!!! product_id is many2many so you must pass product_id or if you want to browse by yourself you must pass product_id.ids

How to pass an external id to a domain filter

I have a Many2One field accepting a product in my model, but I want to limit this field to a specific product template, using the said template external id (The XML id).
I've try this without success:
#This piece of code doesn't work
the_product = fields.Many2one('product.product',
domain = [('product_tmpl_id','=', "ref('the_package.the_external_id')")])
How can I do this?
The solution to this problem is to use a function that returns the filters parameters. That way, we have access to the self variable in the body of the function, and therefore, we can use it to search specific external ids.
#api.model
def _get_domain(self):
# We have access to self.env in this context.
ids = self.env.ref('the_package.the_external_id').ids
return [('product_tmpl_id','=', ids)]
the_field = fields.Many2one('product.product',
required=False,
domain = _get_picking_product_domain)
The above solution does does not work.
Error :
ProgrammingError: can't adapt type 'model_name'
But try this:
You don't have to insert domain in your field :
#api.onchange('field_a')
def fiel_change(self):
dom['field_a'] = [(*)] #* type your domain
return {'domain':dom}

How to get create and edit option in dropdown in openerp

i have separate drop down field and at present i have given static values. i need to get dynamic values like a user should able to add the values and should show in the drop down.
Thanks in advance.
You can define many2one field for this purpose.
_columns = {
'your_field_name' : fields.many2one('object', string='My field')
}
Example
_columns = {
user_id = fields.Many2one('res.users', string=u"User")
}
So you will got list of users. Whatever object list you can define many2one field.

How to create a reference field in OpenERP 6

I am trying to create a field from the web gui of OpenERP and field type as reference
1st there is no better docs about reference
2nd what I want is when someone selects the field it should give another option on selection which is not happening (though it is giving some field but 2nd field throws an error)!
It throws an error object does not exist
Reference fields are mainly used for showing different model's records as reference in your record. For example you have created a model such that whenever a sale order, purchase order, delivery order, project etc are created and saved, then a new record with data like user name, date, some notes should be created in your model. So here you add a reference field which link to the original record(sale order, purchase order etc) from which your record is created. You can find this in res.request model in openerp 6
To create an reference field in your class
def _get_selection_list(self, cr, uid, context=None):
##return a list of tuples. tuples containing model name and name of the record
model_pool = self.pool.get('ir.model')
ids = model_pool.search(cr, uid, [('name','not ilike','.')])
res = model_pool.read(cr, uid, ids, ['model', 'name'])
return [(r['model'], r['name']) for r in res] + [('','')]
_columns = {
'ref': fields.reference(Reference', selection=_get_selection_list, size=128)
}