referenced in foreign key constraint does not exist in Many2many relation in odoo 15 - odoo-15

Here is Model definition:
courier.cod.settings
from odoo import fields,models
class CodSetting(models.Model):
_name = 'courier.cod.settings'
_description = 'Code Settings'
merchant_id = fields.Many2one('res.partner', 'Merchant')
pickup_point_groups_ids = fields.Many2many('courier.pickup.point.groups', string='Pickup Point')
delivery_point_groups_ids = fields.Many2many('courier.delivery.point.groups', string='Delivery Point')
cod_charge = fields.Float('Cod Charge')
minimum_cod_charge = fields.Float('Minimum Cod Charge')
enabled = fields.Boolean(string="Active", default=True)
courier.pickup.point.groups
from odoo import fields,models
class PickupPointGroup(models.Model):
_name = 'courier.pickup.point.groups'
_description = 'Pickup Point Grouping'
title = fields.Char(string='Group Name', required=True, translate=True)
upazilla_ids = fields.Many2many('courier.upazillas','upazilla_id', string='Upazilla')
pickup_point_groups_id = fields.Many2one('courier.cod.settings', 'Cod Setting Line')
courier.delivery.point.groups
from odoo import fields,models
class DeliveryPointGroup(models.Model):
_name = 'courier.delivery.point.groups'
_description = 'Delivery Point Grouping'
title = fields.Char(string='Group Name', required=True, translate=True)
upazilla_ids = fields.Many2many('courier.upazillas','upazilla_id', string='Upazilla')
delivery_point_groups_id = fields.Many2one('courier.cod.settings', 'Cod Setting Line')
When I remove courier.delivery.point.groups everything works fine but when I add this as like courier.pickuup.point.groups I am getting https://prnt.sc/5sAnsTVCv6Ck this error. I am using odoo 15. Any idea?

Related

Get and display many2one line ids record

I have model structure as
class VendorEvaluationTemplate(models.Model):
_name = 'vendor.evaluation.template'
_description = 'Vendor Evaluation Template'
name = fields.Char(string='Name', required=True)
evaluation_lines = fields.One2many('vendor.evaluation.template.line', 'evaluation_template_id', string='Vendor Evaluation Template Line')
class VendorEvaluationTemplateLine(models.Model):
_name = 'vendor.evaluation.template.line'
_description = 'Details Vendor Evaluation Template'
evaluation_template_id = fields.Many2one('vendor.evaluation.template', string='Evaluation Template', ondelete="cascade")
name = fields.Char(string='Name', required=True)
class VendorEvaluation(models.Model):
_inherit = 'vendor.evaluation'
evaluation_template = fields.Many2one('vendor.evaluation.template', string='Evaluation Template')
Now the requirement is when i select evaluation_template, the related lines name from vendor.evaluation.template.line should display below this field.
Following code will work if you only want to display related lines for information purposes. Ref related fields
evaluation_template_lines = fields.One2many(
related='evaluation_template.evaluation_lines',
string="Vendor Evaluation Template Line"
)

Odoo - Filter Many2one not by _rec_name

I would like to filter m2o field, but not by default name (_rec_name).
class LecturerWorkday(models.Model):
_name = 'lecturer.workday'
_rec_name = 'lecturer_id'
name = fields.Selection([('sunday','Sunday'),('monday','Monday'),('tuesday','Tuesday'),
('wednesday','Wednesday'),('thursday','Thursday'),('friday','Friday'),('saturday','Saturday'),
], default='sunday',string="Workday", required=True)
lecturer_id = fields.Many2one('school.lecturer', string="Lecturer Name", invisible=True)
class SchoolLecturer(models.Model):
_name = 'school.lecturer'
name = fields.Char(string="Lecturer Name", required=True)
workday_id = fields.Many2one("lecturer.workday", string="Workday ID")
class LecturerTimeoff(models.Model):
_name = "lecturer.timeoff"
lecturer = fields.Many2one('school.lecturer', string="Lecturer Name")
day_m2o = fields.Many2one('lecturer.workday', string="Lecturer Workdays")
reason = fields.Char("Time off Reason")
#api.onchange('lecturer')
def get_lecturer_workday(self):
day_obj = self.env['lecturer.workday'].search([('lecturer_id', '=', self.lecturer.id)]).mapped('name')
day_list = []
for rec in day_obj:
day_list.append(rec)
res = {}
res['domain'] = {'day_m2o': [('name', '=', day_list)]}
return res
print (res)
My question are:
When I choose lecturer name, day_m2o should display the workday of selected lecturer name. I have been trying to compute it as above, but the result is still display lecturer name, instead of workday.
It seems that #api.onchange didn't update the result instantly whenever i clicked the new lecturer name who didn't have workday yet. How to fix this?
Thanks for your help

How to track One2many field in Odoo12?

I am trying to log the changes on a One2many field using track_visibility='onchange'. But it's not working.
Here is the code:
respartner.py
bank_account_ids = fields.One2many('customer.bank.account','partner_id',
string='Account',track_visibility="onchange")
account.py
_name = 'customer.bank.account'
_description = 'Partner Bank Account Details'
partner_id = fields.Many2one('res.partner',string="Partner")
name = fields.Integer(string="Account Number",required=True,
track_visibility="onchange")
bank_id = fields.Many2one('partner.bank',string="Bank",track_visibility="onchange")
branch_id = fields.Many2one('partner.bank.branch',string="Branch",
track_visibility="onchange")
Yes, No need to touch ORM. Try this
class ParentClass(models.Model):
_name = 'parent.class'
_inherit = ['mail.thread']
child_ids = fields.One2many('child.class', 'relational_field_name_id')
class ChildClass(models.Model):
_name = 'child.class'
_inherit = ['mail.thread']
name = fields.Char(tracking=True) # Note that tracking is true here
relational_field_name_id = fields.Many2one('parent.class')
def write(self, vals):
super().write(vals)
if set(vals) & set(self._get_tracked_fields()):
self._track_changes(self.relational_field_name_id)
def _track_changes(self, field_to_track):
if self.message_ids:
message_id = field_to_track.message_post(body=f'<strong>{ self._description }:</strong> { self.display_name }').id
trackings = self.env['mail.tracking.value'].sudo().search([('mail_message_id', '=', self.message_ids[0].id)])
for tracking in trackings:
tracking.copy({'mail_message_id': message_id})
If you just want to track on relational and not current model then use write instead of copy method
tracking.write({'mail_message_id': message_id})
And for delete and create you can just use message_post inside create and unlink method
self.message_post(body=f'<strong>{ self._description }:</strong> { self.display_name } created/deleted')

About the onchange method returning domain or warning in odoo8

I tried following the odoo8 official example,but not works for me:
class TestMove(models.Model):
_name = 'test.move'
_description = 'Stock Requisition Return'
num1 = fields.Float(string='num1')
num_float = fields.Float(string='num_float', compute="_compute_num_float", store=True)
num2 = fields.Integer(string='num2')
age = fields.Integer(compute='_compute_age')
age2 =fields.Integer(compute='_compute_age2', store=True)
now_list_price = fields.Float(
string='Now List Price', help='Now List Price', digits=(10, 10))
product_id = fields.Many2one(comodel_name='product.product')
......
#api.one
#api.onchange('num1')
def _onchange_num2(self):
now_ = self.num1 + 1
_logger.info('num2 is:%s', now_)
self.update({'num2': now_})
return {
'domain': {'product_id': [('id', 'in', [now_])]},
'warning': {'title': "Warning", 'message': "What is this?"}
}
class ProductProduct(models.Model):
_name = 'product.product'
_description = 'Product'
_rec_name = 'name'
name = fields.Char()
price_unit = fields.Float(default=Decimal('13.14'))
num2 has changed, but domain and warning are not returned;
Any suggestion please. Thanks in advance.
The issue is caused by #api.one decorator, just remove it and the onchange method should work correctly.

odoo how to show the values of my two fields once I do a select

I have two models:
medical.lab.test.type
This is my class:
class MedicalLabTestType(models.Model):
_name = "medical.lab.test.type"
_description = "Medical Lab Test Types"
name = fields.Char(
'Test',
help='Name of test type, such as X-Ray, Hemogram, Biopsy, etc.',
required=True,
)
code = fields.Char(
'Code',
size=128,
help='Short name or code for test type.',
required=True,
)
description = fields.Text('Description')
Test_Prix = fields.Float(
string='Price of the test',
required=True,
)
Nbr_days = fields.Integer(
string='Number of days to have results',
required=True,
)
and
medical.lab
This is my class:
class MedicalLab(models.Model):
_name = 'medical.lab'
_description = "Medical Labs"
test_type_id = fields.Many2one(
string='Test Type',
comodel_name='medical.lab.test.type',
help='Lab test type.',
)
physician_id = fields.Many2one(
string='Pathologist',
comodel_name='medical.physician',
help='Pathologist that performed the exam.',
)
request_physician_id = fields.Many2one(
string='Requesting Physician',
comodel_name='medical.physician',
help='Physician that requested the exam.',
)
The problem is to show on the view the value of Test_Prix and Nbr_days once I choose a Test
How should i proceed?, should I use an onchange function!!!
to show field of selected m2o on the current view you should use
related field.
in your midecal.lab model add:
# related field should always keep the same type Foalt Float, Char Char
# and it's recommended that you put readonly because if you edit
# the value the value will be edited in the related record when you save
Test_Prix = fields.Float(
retlated='test_type_id.Test_Prix',
readonly=True
)
Nbr_days = fields.Integer(
retlated='test_type_id.Nbr_days',
readonly=True
)
and now you can add this two field to you view like
NB: related field are not stored in database they work as proxy if you
want to create the field in the database use store=True
EDITS :
use onchange not compute field is computed
date_perform = fields.Datetime( string='Date of Results', )
#api.onchange('date_request', 'Nbr_days')
def _compute_date_result(self):
for record in self:
business_days_to_add = record.Nbr_days
current_date = fields.Datetime.from_string(record.date_request)
.....