I have a model defined as-
class ModelName(models.Model):
_name = 'model_name'
student_id = fields.Many2one('op.student', 'Student')
I want that student should be unique. So i added -
_sql_constraints = [
('student_unique',
'UNIQUE(student_id)', 'Some message!')
]
but it's not doing anything. I can still select same student and save the form.
I want when i click create button, students for which record has already been saved should not be shown.
What can i do to achieve this??
Try this below domain function in your field, I'm sure you will get your required output.
student_id = fields.Many2one('op.student',domain=lambda self: self.get_not_existing_student_id(), string='Student')
#api.model
def get_not_existing_student_id(self):
self.env.cr.execute("select id from op_student where id not in (select student_id from model_name)")
datas = self.env.cr.fetchall()
student_ids = []
for record in datas:
student_ids.append(record[0])
return [('id','in',student_ids)]
Other Way:
student_id = fields.Many2one('op.student',string='Student')
put it in a view like:
<field name="student_id" context="{'find_existed':1}" options="{'no_create':1}"/>
Then inherit the method name_get() in op.student model like this.
class op_student(models.Model):
_inherit ="op.student"
#api.multi
def name_get(self):
if self._context.get("find_existed",False):
self.env.cr.execute("select id from op_student where id not in (select student_id from model_name)")
datas = self.env.cr.fetchall()
student_ids = []
for record in datas:
student_ids.append(record[0])
for student in self:
if student in student_ids:
res.append((student.id,student.name))
else:
res=super(op_student,self).name_get()
return res
Related
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
http://ludwiktrammer.github.io/odoo/custom-settings-odoo.html
in this article he did
class YourSettings(models.TransientModel):
_inherit = 'res.config.settings'
_name = 'your.config.settings'
company_name = fields.Char()
company_phone = fields.Char()
#api.model
def get_default_company_values(self, fields):
company = self.env.user.company_id
return {
'company_name': company.name,
'company_phone': company.phone,
}
#api.one
def set_company_values(self):
company = self.env.user.company_id
company.name = self.company_name
company.phone = self.company_phone
i want to make company_name = fields.Char() to be company_name = fields.many2one('res.partner','company name')
but the result in employee like res.partner(9,) how to make it show the name of the company
second
i create custom field person height ,person weight i want to add this 2 fields to this custom settings
Put fields of an employee in accounting through a module
Hi. I am creating a module for a client that has a specific need. He wants us to add a price per hour to employees. That is, the price charged by the employee each hour of work. Then create a small expense report that goes into accounting.
The first thing I have done is to modify the employee module using _inherit to add two fields. Nickname that allows employees to filter by nickname. And the hourly price of that employee (what this employee charges for every hour).
employee example changes
The second has been to create a new model that allows adding employees and importing said data. In addition to adding a description.
form example
The challenge now is to link this information to the accounting module so that it will appreciate as a Journal Item and then have the copy confirmed to be appreciated as a Journal Entries.
I am really new in the development of odoo and there are many things that I am still assimilating. So the questions I have are the following:
How could I do this?
Do I have a problem with what I have done so far?
It is my first post and I would appreciate the help. Thanks in advance.
This its the code:
class EmpleadoObra(models.Model):
_inherit = 'hr.employee'
apodo = fields.Char('apodo', readonly=False, store=True)
precio_por_hora = fields.Float('Salario por hora', store=True)
#api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
args = args or []
recs = self.browse()
if name:
recs = self.search(['|', ('apodo', 'ilike', name), ('name', operator, name) ] + args, limit=limit)
return recs.name_get()
class EmpleadosProductos(models.Model):
_name = "employee.as.product"
# _inherits = {'hr.employee' : 'empleado_id'}
employee_line = fields.One2many(
'employee.line',
'id',
string='Employee Lines'
)
class EmployeLine(models.Model):
_name = 'employee.line'
descripcion = fields.Text(string='DescripciĆ³n', required=False)
employee_id = fields.Many2one(
'hr.employee',
string="Empleado",
requiered=True,
change_default=True
)
apodo = fields.Char('apodo', readonly=False)
precio_por_hora = fields.Float('precio_por_hora')
_rec_name = 'apodo'
#api.onchange('employee_id')
def onchange_employee_id(self):
addr = {}
if not self.employee_id.display_name:
return addr
if not self.employee_id.apodo:
self.apodo = "no apodo"
else:
self.apodo = self.employee_id.apodo
self.precio_por_hora = self.employee_id.precio_por_hora
return addr
I've the following model, and the extend to the product_template
class Version(models.Model):
_name='product_cars_application.version'
name = fields.Char()
model_id = fields.Many2one('product_cars_application.model',string="Model")
brand_id = fields.Char(related='model_id.brand_id.name',store=True,readonly=1)
year_id = fields.Char(related='model_id.year_id.name',store=True,readonly=1)
from openerp.osv import osv,fields as Fields
class product_template(osv.osv):
_name = 'product.template'
_inherit = _name
_columns = {
'versions_ids':Fields.many2many('product_cars_application.version',string='Versions')
}
And the following controller which I need to filter products by version_id
#http.route('/pa/get_products/<version_id>', auth='none', type='json',website=True)
def get_products(self,version_id,**kwargs):
#TODO APPEND SECURITY
version_id = int(version_id)
products = http.request.env['product.template'].sudo().search([(version_id,'in','versions_ids')])
I get none products in return while the version_id is in versions_ids.
Do anyone knows what I'm doing wrong?
I need to make the value of comparison of the field a list, maybe becouse the field versions_ids is a many2many
I have solved like this:
#http.route('/pa/get_products/<version_id>', auth='none', type='json',website=True)
def get_products(self,version_id,**kwargs):
#TODO APPEND SECURITY
products = http.request.env['product.template'].sudo().search([('versions_ids','in',[version_id])])
list = []
for p in products:
list.append([p.id, p.name])
return {
'products':list,
}
"return products.ids" is missing inside get_products like:
#http.route('/pa/get_products/<version_id>', auth='none', type='json',website=True)
def get_products(self,version_id,**kwargs):
#TODO APPEND SECURITY
version_id = int(version_id)
products = http.request.env['product.template'].sudo().search([(version_id,'in','versions_ids')])
return products.ids
I have fields for a quick access of the entities related to a project. For example:
class project(models.Model):
_name = "project.project"
_description = "Project"
_inherit = 'project.project'
production_order_ids = fields.One2many('mrp.production', 'project_id', 'Production orders')
purchase_order_ids = fields.One2many('purchase.order', 'project_id', 'Purchase orders')
....
I am trying to create a sale_order_ids in the project.project model. My first try did not work:
sale_order_ids = fields.One2many('sale.order', 'project_id', string='Sale orders')
because because the field sale.order.project_id is of type account.analytic.account.
A project.project object inherits from account.analytic.account. This query should work ok if they would share the same Id, but they do not. So the navigation would be:
"project.project".analytic_account_id" -> sale.order".project_id
And the result would be the corresponding sale.order(s).
Use a computed field:
sale_order_ids = fields.One2many('sale.order', compute='_get_sale_orders', string='Sale orders')
#api.model
def _get_sale_orders(self):
for record in self:
record.sale_order_ids = self.env['sale.order'].search([('project_id', '=', record.analytic_account_id.id)]).ids