How to pass an external id to a domain filter - odoo

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}

Related

how to set domain with python in odoo

Is it possible to set domain on the python side without putting it in view?
I tried to set the domain in the customer field (sale order) based on branch in the Customer menu/template
I have tried this but why it doesn't work ?
#api.onchange('partner_id')
def _onchange_cust_domain(self):
for rec in self:
if self.branch_id.id:
cust_list = rec.partner_id.branch_id.id
return {'domain' : {'partner_id' : [('branch_id', '=', cust_list)]}}
To set the partner_id domain when the branch_id field value changes, use the following onchange function:
Example:
#api.onchange('branch_id')
def onchange_branch_id(self):
if self.branch_id:
return {'domain': {'partner_id': ['|', ('branch_id', '=', False), ('branch_id', '=', self.branch_id.id)]}}
Odoo sets by default the following domain from the python code:
['|', ('company_id', '=', False), ('company_id', '=', company_id)]
I think your problem is that you are making a domain for a field partner_id, but in the onchange method of the same field.
It makes no sense to me. So if you put a Customer with branch X, then you can only search customers of branch X for that sale_order (if that domain do the job).
I dont know what are you trying to do exactly, but i think than changing a domain in the same field you are changing the value it is no possible.
Your code would be correct if that domain applies to another field.
This is a working exaple. Notice that onchange field method and field to apply domain are diferent:
#api.onchange('operating_unit_id')
def _applicants_onchange(self):
if self.operating_unit_id:
return {'domain': {'applicant_id': [('default_operating_unit_id', '=', self.operating_unit_id.id)]}}
return {}
Returning a domain using onchange method has been deprecated in Odoo 14.
You can use the new way used by Odoo:
Returning a domain using onchange method has been deprecated in Odoo 14.
You can use this new way.

(Odoo) Store field names into a selection field of a separate model

How should I get all of the field names from the product.template model and store them in a selection field in a separate model?
Example
Thanks
I manage to fix the issue with the code below, I tried using self in the method get_fields but the following KeyError: product.template occurred so too bypass that I used request instead of self.
The selection field consists of [(product_template_field_name_1, product_template_field_name_string_1),..,(product_template_field_name_n, product_template_field_name_string_n)], n being the number of fields in the product.template model.
from odoo.http import request
def get_fields(self):
fields = [(field, request.env['product.template']._fields[field].string) for field in
request.env["product.template"]._fields]
return fields
field_name = fields.Selection(
selection=lambda self: self.get_fields(),
required=True)

Get value from customer_entity_varchar - Magento

I created a customer attribute in backend magento, but I want to show this attribute to the user so that he can alter its value in the frontend. My input field is being displayed in the frontend, but the value is not there. I am not able to get this value. I found that the value that I need to display is in the apscustomer_entity_varchar table and the column is called 'value'. How can I get that value from that table? I was trying this:
$collection = Mage::getModel('eav/entity_attribute')->getCollection();
foreach ($collection as $data) {
return $data;
}
but it was not working, so I used SQL code and it worked. However I know that's not a nice way to do that in Magento. What I did was something like:
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT * FROM `apscustomer_entity_varchar ` WHERE `entity_id`='$id'";
$rows = $connection->fetchAll($sql);
How can I get the value column from my apscustomer_entity_varchar table in the magento way, using the getModel?
You need to retrieve the customer of the session and then you can get the attribute you want :
Mage::getSingleton('customer/session')->getData('attributeName');
Mage::getSingleton('customer/session')->getAttributeName(); //Magic getter
Where attributeName is the name of the attribute you want to get the value.
I found out how to do that :D
It's actually very simple. Even if this is a custom customer attribute (created in Magento admin panel), I need to get the attribute not using the 'eav/entity_attribute' model, but using the 'customer/customer' model. So I just need to do that:
$customer_data = Mage::getModel('customer/customer')->load($customer_id)->getData();
echo $customer_data['attributeThatYouWant'];
If you are not sure what is the name of your attribute you can look at Magento Admin Panel under Customer/Manage-Attributes, or you can use that after getting the model:
var_dump($customer_data);
Now you are able to get the name of all customer attributes.

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 get data is on a remote server using 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.