I have 2 fields which point to 2 different models.
class ResPartnerSchool(models.Model):
_name = 'ecole.partner.school'
foyer_id = fields.Many2one(string="Foyer", comodel_name="horanet.relation.foyer",
domain="[('partner_id','=', partner_id)]", store=True, readonly=False)
partners_in_foyer_id = fields.Many2one(string="Name", comodel_name="res.partner", readonly=False, store=True)
I will like to recover the partners who are in the home that I recovered from my form.
I managed to do an onchange () function to automatically retrieve one of the household members.
#api.onchange('foyer_id')
def get_partners_foyer(self):
if self.foyer_id:
foyer = self.foyer_id.foyer_id
domain = [('foyer_id', '=', foyer.id)]
members = self.foyer_id.search(domain, limit=1)
if not members :
_logger.info(f"Pas de membres trouvés pour : {self.partner_id.name}")
else:
if members.partner_id:
self.partners_in_foyer_id = members.partner_id.id
But how do you get all the household members and display them in the xml view? Currently, all the partners of the res.partner table are retrieved in the view.
Thanks for your help !
First you put one flag(boolean type) in res.partner for home, and then use this domain (domain="[('home','=', True)]") in your Many2one(partners_in_foyer_id) field.
Related
I have an inherited model from res.partner.bank which i added a custom field called location to, this is how the model looks like.
class SalesPartnerBankInherit(models.Model):
_inherit = 'res.partner.bank'
location = fields.Char()
I have another inherited model from sale.order with a field that has a many2one relationship to res.partner.bank
I want to show only 1 record on the many2one dropdown if there are more than 1 records in res.partner.bank that has the same location field value?
This is my inherited sale.order model with the many2one location_id field
location_id = fields.Many2one('res.partner.bank',
string='Bank Account Location')
So if there are duplicate res.partner.bank records with the same location, I want to show only 1 record on the dropdown.
First, make your field compute then in compute method search all record from specific model and assign as per your requirement.
location_id = fields.Many2one('res.partner.bank', string='Meters', compute="_get_value")
def _get_value(self):
for i in self:
records = self.env['res.partner.bank'].search([], limit=1) # search with one record only from whole search and assign it.
i.location_id = records and records.id
I have a entity "pay_activity" with an antivity (activity_id) and a purchase (purchase_id).
The purchase has a list of activities related to that purchase (activity_ids).
I want to apply a domain to activity, so the user can only select an activity in the list of Activities of the purchase (purchase_activity).
I have tried to apply the domain domain=[('id','in',purchase_activity)] to the the activity field but it shows an error "RecursionError: maximum recursion depth exceeded while calling a Python object".
How can I apply domain to the activity_id field to ensure that the user will only select an activity that is in the list of all activiities related to the purchase (purchase_activity)?
This is my code:
class CenterPayActivity(models.Model):
_name = 'center.pay_activity'
_description = 'Permite distribuir los pagos entre las actividades'
purchase_id = fields.Many2one('center.purchase', string='pago')
purchase_activity = fields.Many2many('center.activity', related="purchase_id.activity_ids")
activity_id = fields.Many2one('center.activity', string='Actividad', required=True, domain=[('id','in',purchase_activity)] )
String domains are supposed to be dynamic and evaluated on the client-side only.
Use the field domain in the the form view and it will apply when displaying existing records for selection:
<field name="activity_id" domain="[('id', 'in', purchase_activity)]"/>
I am creating a module something like call log. In that i need to search the customer number and get the Partner information or have to link the partner automatically.
the following are the codes, somebody please help me.
class model_call(models.Model):
_inherit = 'res.partner'
_name = 'model.call'
_description = 'call logs'
""" *************** Base Fields ******************* """
name = fields.Char(string='Topic')
customer_number = fields.Char(string='Customer Number', track_visiility='onchange', onchange='get_partner(customer_number)')
#-------------------------------------------------
# apis
#-------------------------------------------------
#api.onchange('customer_number')
def get_partner(self, customer_number):
if customer_number:
customer = self.env['res.partner'].search([('customer_number', '=', record.phone)])
return customer
#------------------------------------------------------
customer = fields.Many2one('res.partner', string='Customer', track_visibility='onchange',index=True, help="Linked partner (optional). Usually created when converting the lead.",)
Your onchange isn't correct. You don't need parameters and have to return a dictionary or nothing. The dictionary is only needed for field filtering changes and warning messages. Value changes like yours are made "directly":
#api.onchange('customer_number')
def get_partner(self):
if self.customer_number:
customer = self.env['res.partner'].search(
[('customer_number', '=', self.phone)]) # shouldn't it be self.customer_number?
self.customer = customer
And try to stick to the Odoo Guidelines and change the field customer to customer_id.
I have the following problem I am creating a form that is a budget replica, but this type of budget does not carry VAT% and good will not pass through accounting.
Well the problem is the following I have created a model called budget.table
It is the following :
class TableElements(models.Model):
_name = 'budget.table'
product_id = fields.Many2one('product.product', string='Product',ondelete='restrict', index=True)
name = fields.Text(string='Description', required=True)
quantity = fields.Float(string='Quantity',required=True, default=1)
price_unit = fields.Float(string='Unit Price', required=True,)
price_subtotal = fields.Float(string='Amount',store=True, readonly=True)
and I have another model called budget.two which is the following:
class BudgetTwo(models.Model):
_name = 'budget.two'
name = fields.Char(string ='Nombre', copy=False, index=True ,default ="Nuevo")
partner_id =fields.Many2one('res.partner' ,string ='Cliente', copy=False, index=True,required=True)
deliver_date = fields.Date(string ='Fecha de Entrega')
expiration_date = fields.Date(string ='Fecha de expiración')
pay_place =fields.Many2one('account.payment.term' ,string='Plazo de Pago')
order_line = fields.One2many('budget.table','id' ,string = 'Pedidos' )
total = fields.Float(string = 'Total:' ,compute="_total")
Well I want: as you can see in 'budget.two' there is a One2Many field which I will add all the new products that in turn will be saved in this type of new budget that I am created as I already commented without VAT and it will not happen by the accounting module.
When I select the products that I am going to save the One2manny, I keep it blank. Example:
So it should be kept:
but when you save it, look how it is stored without any element in the One2MAny field:
[![enter code here][2]][2]
In 'budget.table' add this field:
budget_two_id = fields.Many2one('budget.two')
In 'budget.two' correct this field:
order_line = fields.One2many('budget.table', 'budget_two_id', string='Pedidos')
The point is any One2many field should have an inverse field (Many2one) on the other model as a foreign key.
I have a problem in ODOO 10 with a many2many relationship.
I have extended res.partner this way:
class ResPartner(models.Model):
x_bagsdesign = fields.Many2many('product.product',string='Bags Design',relation='bags_design_manufactur')
then I have extended also product.template model:
class product_template_fields(models.Model):
_inherit = 'product.template'
x_traders_stock = fields.Many2many(
'res.partner', string='Traders with access to stock',relation='xtradstock_res_partner_rel')
#api.multi
def write(self, vals):
record = super(product_template_fields, self).write(vals)
for singletrader in self.x_traders_stock:
singletrader.x_bagsdesign = [(4,self.id)]
return record
This way every time a new x_traders_stock is inserted in product.template, a new x_bags_design is also created in res.partner.
BUT.. when I save a new record in product.template I get an sql error:
bad query: INSERT INTO bags_design_manufactur (res_partner_id, product_product_id)
(SELECT a, b FROM unnest(ARRAY[1]) AS a, unnest(ARRAY[7]) AS b)
EXCEPT (SELECT res_partner_id, product_product_id FROM bags_design_manufactur WHERE res_partner_id IN (1))
I don't understand where the EXCEPT part of the sql query is coming from and how to prevent it. If anyone can help I would be grateful.. thanks!
The error message is a bit different from expected, but I can fix some problems in your code. First of all you have to take into account that a product.product object is a variant of a product.template object, so you can have in a database many product.product objects pointing to the same product.template (e.g. a product.template is a T-shirt and a product.product is a T-shirt colour red size M). This means that you can't try to set the ID of a product.template in a field which is expecting the ID of a product.product, as you're doing here:
singletrader.x_bagsdesign = [(4,self.id)]
Of course, that mistake will not give you the message error you are receiving, there must be something wrong in other part of your code (I guess related to bags_design_manufactur model).
However, to fix the problem I told you above, you should write this:
class product_template_fields(models.Model):
_inherit = 'product.template'
x_traders_stock = fields.Many2many(
comodel_name='res.partner',
string='Traders with access to stock',
relation='xtradstock_res_partner_rel'
)
#api.multi
def write(self, vals):
result = super(product_template_fields, self).write(vals)
for prod_templ in self:
products = self.env['product.product'].search([
('product_tmpl_id', '=', prod_templ.id),
])
for singletrader in prod_templ.x_traders_stock:
singletrader.write({
'x_bagsdesign': [(4, product.id) for product in products],
})
return result
EDIT
product.product inherits from product.template by delegation, this means that every field you create in product.template model is going to be available in product.product objects, so when you are creating the Many2many field x_traders_stock in product.template, you're creating it in product.product too, so you don't need to add records each time a x_trader is generated. Instead you should change your models:
class ResPartner(models.Model):
x_bagsdesign_prod_templ = fields.Many2many(
comodel_name='product.template',
column1='partner_id',
column2='product_tmpl_id',
string='Bags Design',
relation='xtradstock_res_partner_rel'
)
class ProductTemplate(models.Model):
_inherit = 'product.template'
x_traders_stock = fields.Many2many(
comodel_name='res.partner',
column1='product_tmpl_id',
column2='partner_id',
string='Traders with access to stock',
relation='xtradstock_res_partner_rel'
)
And then, if you want to access to the product.product objects a partner has, you can do it this way:
any_partner.x_bagsdesign_prod_templ.mapped('product_variant_ids')
If you preferred it, you could even create a new related field in res.partner which brought the product.product objects a partner has.