Odoo: ValueError("Expected singleton: %s" % self) - odoo

I'm modifying Odoo OpenEduCat exam module to fit the need of my institution. For that, I have tailored the code as shown below. However,when I click on generate button, odoo raises expected singleton error. Generating button
Error details
--Python code--
from openerp import models, fields, api
class OpResultTemplate(models.Model):
_name = 'op.result.template'
_description = 'Result Template'
_rec_name = 'name'
exam_session_id = fields.Many2one(
'op.exam.session', 'Exam Session', related='line_ids.exam_session_id', required=False)
name = fields.Char("Name", size=254, required=True)
result_date = fields.Date(
'Result Date', required=True, default=fields.Date.today())
line_ids = fields.One2many(
'op.result.template.line', 'result_id', 'Session Lines')
####this is for semester
inter1_ids = fields.One2many(
'op.internal1', 'result_id', 'Internal 01')
inter2_ids = fields.One2many(
'op.internal2', 'result_id', 'Internal 02')
model_ids = fields.One2many(
'op.model', 'result_id', 'Model')
final_ids = fields.One2many(
'op.final', 'result_id', 'Semester')
state = fields.Selection(
[('normal', 'Normal'), ('semester', 'Semester')],
string='State', required=True, default='normal')
# pass_status_ids = fields.Many2many('op.pass.status', string='Pass Status')
#api.one
def generate_result(self):
data = self.read(['state'])[0]
if data['state'] == 'normal' :
####Write information in to Marksheet Register the place where result generate to.
marksheet_reg_id = self.env['op.marksheet.register'].create({
'name': 'Mark Sheet for %s' % self.line_ids.exam_session_id.name,
'exam_session_id': self.line_ids.exam_session_id.id,
'generated_date': fields.Date.today(),
'generated_by': self.env.uid,
'status': 'draft',
'course_id': self.line_ids.exam_session_id.course_id.name,
'batch_id': self.line_ids.exam_session_id.batch_id.name,
'exam_type': self.line_ids.exam_session_id.exam_type.name,
'semester_id': self.line_ids.exam_session_id.semester_id.name,
})
student_list = []####Define array to store
for exam_session in self.line_ids:####line_ids is table that located in Result generator which allow to choose exam session
total_exam = 0.0#global var
for exam in exam_session.exam_session_id:####exam_session.exam_lines is the table that list the exam or subject located in Result generator->Exam session
total_exam += exam.exam_ids.total_marks
for attd in exam.exam_ids.attendees_line:####exam.exam_id.attendees_line location that contant student name and mark in each subject
result_dict = {####this loop is to write information to result line
'exam_id': exam.exam_ids.id,
'exam_tmpl_id': exam.exam_ids.id,
'marks': attd.marks,####IMPORTANCE mark that student get in each subject THIS IS WHERE TO APPLY PERCENTAGES
'status': attd.marks >= exam.exam_ids.min_marks and####IMPORTANCE take the mark and decide pass or fail base on passing mark in each subject
'pass' or 'fail',
'per': (100 * attd.marks) / exam.exam_ids.total_marks,####NOT IMPORTANCE this can be delete, this take the mark student get and find the percentage of the subject student get in each subject
'student_id': attd.student_id.id,####student name
'total_marks': exam.exam_ids.total_marks,####the total mark of each subject that have been enter when created subject for exam
}
--Error details--
Odoo Server Error
Traceback (most recent call last):
File "/home/v4d/odoo/openerp/http.py", line 650, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/v4d/odoo/openerp/http.py", line 687, in dispatch
result = self._call_function(**self.params)
File "/home/v4d/odoo/openerp/http.py", line 323, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/v4d/odoo/openerp/service/model.py", line 118, in wrapper
return f(dbname, *args, **kwargs)
File "/home/v4d/odoo/openerp/http.py", line 316, in checked_call
result = self.endpoint(*a, **kw)
File "/home/v4d/odoo/openerp/http.py", line 966, in call
return self.method(*args, **kw)
File "/home/v4d/odoo/openerp/http.py", line 516, in response_wrap
response = f(*args, **kw)
File "/home/v4d/odoo/addons/web/controllers/main.py", line 899, in call_button
action = self._call_kw(model, method, args, {})
File "/home/v4d/odoo/addons/web/controllers/main.py", line 887, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "/home/v4d/odoo/openerp/api.py", line 250, in wrapper
return old_api(self, *args, **kwargs)
File "/home/v4d/odoo/openerp/api.py", line 421, in old_api
result = new_api(recs, *args, **kwargs)
File "/home/v4d/odoo/openerp/api.py", line 425, in new_api
result = [method(rec, *args, **kwargs) for rec in self]
File "/home/v4d/odoo/addons/openeducat_exam/models/result_template.py", line 71, in generate_result
total_exam += exam.exam_ids.total_marks
File "/home/v4d/odoo/openerp/fields.py", line 821, in get
record.ensure_one()
File "/home/v4d/odoo/openerp/models.py", line 5432, in ensure_one
raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: op.exam(44, 45, 46)
I have tried other solutions that could be found on the Internet, but it didn't seem to work. Please kindly help me to deal with this.Thank in advance.

Here is the issue in your code,
####IMPORTANCE take the mark and decide pass or fail base on passing mark in each subject
'status': attd.marks >= exam.exam_ids.min_marks and 'pass' or 'fail',
exam.exam_ids it will return list of browsable objects (recordset list) and you are trying to access min_marks properties, so here it gets confused min_marks property from which object. So it raise an error.
So either you need to specify single object by specifying exam.exam_ids[0] (only single object will return) or you need to search proper records from the one2many model and then you can access to the min_marks field.
Properties are separately created for all objects (OOP rule). Static
properties will be accessible via class.

Related

Error can't adapt type 'some.model.'

I have error when try to receive data from column.
Model is:
class int_filial_phone(models.Model):
_name = 'pr_filials.int_filial_phone'
name = fields.Char(string="Partner-number") #, compute='_get_name_field')
number = fields.Char(string="Phone")
active = fields.Boolean(string="Active")
filial_addr_ids = fields.One2many('pr_filials.filial_addr', 'int_filial_phone_id', string='Address')
filial_id = fields.Many2one('res.company', string='Filial')
advert_phone_ids = fields.One2many('pr_filials.advert_phone', 'int_filial_phone_id', 'Advert phone')
_sql_constraints = [
('number_unique',
'UNIQUE(number)',
"The parameter number must be unique"),
]
Methods:
def find_client_in_filial_phone(self, phone, table):
cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
result = None
table = request.env[table]
phone = self.format_symbol_phone(phone)
_logger.error('find_client_in_filial_phone phone: %r ', phone )
ids = table.sudo().search([['number', '=', phone],], limit=1)
if(len(ids)>0):
result = table.sudo().browse(ids)[0]
_logger.error('find_client_in_filial_phone result: %r ', result )
return result
I try to receive record id:
int_phone = self.find_client_in_filial_phone(data[3], 'pr_filials.int_filial_phone')
int_phone_id = int(int_phone.id)
All work fine
When i try to receive another field of record:
_logger.error("PHONE NAME: %r", int_phone[0].name)
I receive error:
Traceback (most recent call last): File
"/home/skif/odoo/openerp/http.py", line 648, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception) File "/home/skif/odoo/openerp/http.py", line 685, in dispatch
result = self._call_function(**self.params) File "/home/skif/odoo/openerp/http.py", line 321, in _call_function
return checked_call(self.db, *args, **kwargs) File "/home/skif/odoo/openerp/service/model.py", line 118, in wrapper
return f(dbname, *args, **kwargs) File "/home/skif/odoo/openerp/http.py", line 314, in checked_call
result = self.endpoint(*a, **kw) File "/home/skif/odoo/openerp/http.py", line 964, in call
return self.method(*args, **kw) File "/home/skif/odoo/openerp/http.py", line 514, in response_wrap
response = f(*args, **kw) File "/home/skif/odoo/openerp/my-addons/pr_finance/controllers/controllers.py",
line 151, in upload_file
_logger.error("PHONE INT : %r", int_phone[0].name) File "/home/skif/odoo/openerp/fields.py", line 830, in get
self.determine_value(record) File "/home/skif/odoo/openerp/fields.py", line 930, in determine_value
record._prefetch_field(self) File "/home/skif/odoo/openerp/api.py", line 248, in wrapper
return new_api(self, *args, **kwargs) File "/home/skif/odoo/openerp/models.py", line 3308, in _prefetch_field
result = records.read([f.name for f in fs], load='_classic_write') File "/home/skif/odoo/openerp/api.py", line 248, in wrapper
return new_api(self, *args, **kwargs) File "/home/skif/odoo/openerp/models.py", line 3238, in read
self._read_from_database(stored, inherited) File "/home/skif/odoo/openerp/api.py", line 248, in wrapper
return new_api(self, *args, **kwargs) File "/home/skif/odoo/openerp/models.py", line 3376, in _read_from_database
cr.execute(query_str, params) File "/home/skif/odoo/openerp/sql_db.py", line 141, in wrapper
return f(self, *args, **kwargs) File "/home/skif/odoo/openerp/sql_db.py", line 220, in execute
res = self._obj.execute(query, params) File "/home/skif/.local/lib/python2.7/site-packages/psycopg2/extensions.py",
line 129, in getquoted
pobjs = [adapt(o) for o in self._seq] ProgrammingError: can't adapt type 'pr_filials.int_filial_phone'
How I can receive data from record? Why i received id but cannot received data from other fields of record?
In the new version of the api, when you used the method "search", the value return is a recordSet.
Example:
Old api version
record_ids = self.pool.get('model.name').search([('name', '=', 'Jon doe')])
# The value of record_ids is like [1,2,3,4]
records = self.pool.get('model.name').browse(records_ids)
# The value of records is like model.name(1,2,3,4)
In the new version of api
records = self.env['model.name'].search([('name', '=', 'Jondoe')])
# The vale of records is like model.name(1,2,3,4)
In your code you try to browse with recordSet.
def find_client_in_filial_phone(self, phone, table):
...
ids = table.sudo().search([['number', '=', phone],], limit=1)
# Here ids is a recordSet
if(len(ids)>0):
result = table.sudo().browse(ids)[0]
_logger.error('find_client_in_filial_phone result: %r ', result )
return result
You must do like this.
def find_client_in_filial_phone(self, phone, table):
cr, uid, context, registry = request.cr, request.uid, request.context, request.registry
table = request.env[table]
phone = self.format_symbol_phone(phone)
_logger.error('find_client_in_filial_phone phone: %r ', phone )
result = table.sudo().search([['number', '=', phone],], limit=1)
_logger.error('find_client_in_filial_phone result: %r ', result )
return result
If your search doesn't find value, an empty recordSet is return.

Odoo unknown currency_field 'currency_id'

In Odoo 9 community version, I added the following monetary field as a monetary amount via the web interface: res.partner.x_tablet_bail_amount.
It's working fine, but when installing any new module I get the following error and my module does not install.
Any help? thanks!
Odoo Server Error
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 605, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 642, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 316, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 118, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 309, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 893, in __call__
return self.method(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 471, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 998, in call_button
action = self._call_kw(model, method, args, {})
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 986, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 238, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/module/module.py", line 459, in button_immediate_install
return self._button_immediate_function(cr, uid, ids, self.button_install, context=context)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 238, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/base/module/module.py", line 533, in _button_immediate_function
registry = openerp.modules.registry.RegistryManager.new(cr.dbname, update_module=True)
File "/usr/lib/python2.7/dist-packages/openerp/modules/registry.py", line 385, in new
openerp.modules.load_modules(registry._db, force_demo, status, update_module)
File "/usr/lib/python2.7/dist-packages/openerp/modules/loading.py", line 283, in load_modules
registry.setup_models(cr, partial=True)
File "/usr/lib/python2.7/dist-packages/openerp/modules/registry.py", line 199, in setup_models
model._setup_fields(cr, SUPERUSER_ID)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 238, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 342, in old_api
result = method(recs, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 3044, in _setup_fields
field.setup_full(self)
File "/usr/lib/python2.7/dist-packages/openerp/fields.py", line 476, in setup_full
self._setup_regular_full(model)
File "/usr/lib/python2.7/dist-packages/openerp/fields.py", line 1131, in _setup_regular_full
"Field %s with unknown currency_field %r" % (self, self.currency_field)
AssertionError: Field res.partner.x_tablet_bail_amount with unknown currency_field 'currency_id'
Fields of type monetary are using other fields to do some stuff like printing currency symbols in reports or the client. Per default currency_id is used (Odoo Code) for that process, and as far as i know there is no currency_id defined on res.partner.
So maybe you should define it or get it as related field from another relation?
From the source code it does not seem to be any currency_id field defined on res.partner.
However the field is indeed defined on res.company, which has a Many2One relationship with res.partner. I suppose res.partner is nothing but a kind of partially abstract class that needs to be extended for every type of partner within Odoo, including companies (only guessing here).
So Odoo is right when it spits
Field res.partner.x_tablet_bail_amount with unknown currency_field
'currency_id'
because the field is not there, really.
What you could do is, instead of defining your custom field x_tablet_bail_amount on res.partner, define it on res.company. That way your monetary field will be able to resolve the right currency instead of looking for something that does not exist.
'currency_id' field is added to 'res.partner' model in the 'account' mudule, so you have to add 'account' to the list of dependencies in the manifest and then the monetary field would work fine.

assign task to multiple users in odoo

in odoo (fromerly openERP) i want to customize Project management module.
currently a task can be assigned to a single user. model code is:
class task(osv.osv):
_name = "project.task"
_description = "Task"
'user_id': fields.many2one('res.users', 'Assigned to', select=True, track_visibility='onchange'),
Now want to assign a task to multiple users. and i have modified user_id field like this:
'user_id': fields.many2many('res.users', 'Assigned to', select=True, track_visibility='onchange'),
but it shows following traceback:
Traceback (most recent call last):
File "/home/hardik/odoo/odoo/openerp/http.py", line 537, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/hardik/odoo/odoo/openerp/http.py", line 574, in dispatch
result = self._call_function(**self.params)
File "/home/hardik/odoo/odoo/openerp/http.py", line 310, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/hardik/odoo/odoo/openerp/service/model.py", line 113, in wrapper
return f(dbname, *args, **kwargs)
File "/home/hardik/odoo/odoo/openerp/http.py", line 307, in checked_call
return self.endpoint(*a, **kw)
File "/home/hardik/odoo/odoo/openerp/http.py", line 803, in __call__
return self.method(*args, **kw)
File "/home/hardik/odoo/odoo/openerp/http.py", line 403, in response_wrap
response = f(*args, **kw)
File "/home/hardik/odoo/odoo/addons/web/controllers/main.py", line 884, in search_read
return self.do_search_read(model, fields, offset, limit, domain, sort)
File "/home/hardik/odoo/odoo/addons/web/controllers/main.py", line 905, in do_search_read
request.context)
File "/home/hardik/odoo/odoo/openerp/http.py", line 908, in proxy
result = meth(cr, request.uid, *args, **kw)
File "/home/hardik/odoo/odoo/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/hardik/odoo/odoo/openerp/models.py", line 5146, in search_read
result = self.read(cr, uid, record_ids, fields, context=read_ctx)
File "/home/hardik/odoo/odoo/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/hardik/odoo/odoo/openerp/models.py", line 3141, in read
result = BaseModel.read(records, fields, load=load)
File "/home/hardik/odoo/odoo/openerp/api.py", line 239, in wrapper
return new_api(self, *args, **kwargs)
File "/home/hardik/odoo/odoo/openerp/models.py", line 3176, in read
self._read_from_database(stored, inherited)
File "/home/hardik/odoo/odoo/openerp/api.py", line 239, in wrapper
return new_api(self, *args, **kwargs)
File "/home/hardik/odoo/odoo/openerp/models.py", line 3354, in _read_from_database
res2 = self._columns[f].get(cr, self._model, ids, f, user, context=context, values=result)
File "/home/hardik/odoo/odoo/openerp/osv/fields.py", line 1012, in get
cr.execute(query, [tuple(ids),] + where_params)
File "/home/hardik/odoo/odoo/openerp/sql_db.py", line 158, in wrapper
return f(self, *args, **kwargs)
File "/home/hardik/odoo/odoo/openerp/sql_db.py", line 234, in execute
res = self._obj.execute(query, params)
ProgrammingError: syntax error at or near "to"
LINE 1: SELECT Assigned to.res_users_id, Assigned to.project_task_id...
^
please guide me how to deal with this. thanks in advance
When we create many2many type field, than we must to give a name of relation table where in that table record will store. In below example 'task_user_rel' is a relational table.
You may try this code:
'user_ids': fields.many2many('res.users', 'task_user_rel', 'user_id', 'id', 'Assigned to', select=True, track_visibility='onchange'),
Now add new user_ids field in xml side and hide/invisible a exists user_id field.

Creating module via web-interface

I followed the following steps to create a module named "x_test" :
1. Settings -->Technical --> Database Structure --> Models --> Create
Model Description : Test
Model :x_test
2. Add Fields
Name: x_sample
Field Label: sample
Field Type: boolean
3. Save
4. Click on Create a Menu
5. Select appropriate menu and click on "CREATE MENU"
6. Click on the menu provided and try to provide values for my custom module
7. When I "SAVE" my record, I am getting the following error:
Traceback (most recent call last):
File "/var/app/openerp/server/openerp/netsvc.py", line 292, in dispatch_rpc
result = ExportService.getService(service_name).dispatch(method, params)
File "/var/app/openerp/server/openerp/service/web_services.py", line 626, in dispatch
res = fn(db, uid, *params)
File "/var/app/openerp/server/openerp/osv/osv.py", line 188, in execute_kw
return self.execute(db, uid, obj, method, *args, **kw or {})
File "/var/app/openerp/server/openerp/osv/osv.py", line 131, in wrapper
return f(self, dbname, *args, **kwargs)
File "/var/app/openerp/server/openerp/osv/osv.py", line 197, in execute
res = self.execute_cr(cr, uid, obj, method, *args, **kw)
File "/var/app/openerp/server/openerp/osv/osv.py", line 185, in execute_cr
return getattr(object, method)(cr, uid, *args, **kw)
File "/var/app/openerp/server/openerp/osv/orm.py", line 4434, in create
cr.execute('insert into "'+self._table+'" (id'+upd0+") values ("+str(id_new)+upd1+')', tuple(upd2))
File "/var/app/openerp/server/openerp/sql_db.py", line 161, in wrapper
return f(self, *args, **kwargs)
File "/var/app/openerp/server/openerp/sql_db.py", line 228, in execute
res = self._obj.execute(query, params)
ProgrammingError: column "x_sample" of relation "x_test" does not exist
LINE 1: insert into "x_test" (id,"x_sample",create_uid,create_date,wri...
Is there any mistake in my regards of creating a module via Web Interface
I had the same issue, go into the database and within the table x_test create the field you added in the model.
Now as far as I am concerned, it should create the fields for you. But I have had to create the fields manually.

How to create a many2one field in OpenERP 7

There are no docs for OpenERP 7 for this: I am trying to create a field with many2one relation from the webclient. I did the following:
1st step created a model and assigned some fields to it
2nd I created a field and mapped the model from the field and when I append that field in view I have been getting this error
Client Traceback (most recent call last):
File "/opt/openerp/web/addons/web/http.py", line 195, in dispatch
response["result"] = method(self, **self.params)
File "/opt/openerp/web/addons/web/controllers/main.py", line 1085, in call_kw
return self._call_kw(req, model, method, args, kwargs)
File "/opt/openerp/web/addons/web/controllers/main.py", line 1077, in _call_kw
return getattr(req.session.model(model), method)(*args, **kwargs)
File "/opt/openerp/web/addons/web/session.py", line 40, in proxy
result = self.proxy.execute_kw(self.session._db, self.session._uid, self.session._password, self.model, method, args, kw)
File "/opt/openerp/web/addons/web/session.py", line 28, in proxy_method
result = self.session.send(self.service_name, method, *args)
File "/opt/openerp/web/addons/web/session.py", line 101, in send
raise xmlrpclib.Fault(openerp.tools.exception_to_unicode(e), formatted_info)
Server Traceback (most recent call last):
File "/opt/openerp/web/addons/web/session.py", line 87, in send
return openerp.netsvc.dispatch_rpc(service_name, method, args)
File "/opt/openerp/server/openerp/netsvc.py", line 361, in dispatch_rpc
result = ExportService.getService(service_name).dispatch(method, params)
File "/opt/openerp/server/openerp/service/web_services.py", line 601, in dispatch
res = fn(db, uid, *params)
File "/opt/openerp/server/openerp/osv/osv.py", line 167, in execute_kw
return self.execute(db, uid, obj, method, *args, **kw or {})
File "/opt/openerp/server/openerp/osv/osv.py", line 121, in wrapper
return f(self, dbname, *args, **kwargs)
File "/opt/openerp/server/openerp/osv/osv.py", line 176, in execute
res = self.execute_cr(cr, uid, obj, method, *args, **kw)
File "/opt/openerp/server/openerp/osv/osv.py", line 164, in execute_cr
return getattr(object, method)(cr, uid, *args, **kw)
File "/opt/openerp/server/openerp/osv/orm.py", line 2389, in name_search
return self._name_search(cr, user, name, args, operator, context, limit)
File "/opt/openerp/server/openerp/osv/orm.py", line 2420, in _name_search
ids = self._search(cr, user, args, limit=limit, context=context, access_rights_uid=access_rights_uid)
File "/opt/openerp/server/openerp/osv/orm.py", line 4773, in _search
query = self._where_calc(cr, user, args, context=context)
File "/opt/openerp/server/openerp/osv/orm.py", line 4622, in _where_calc
e = expression.expression(cr, user, domain, self, context)
File "/opt/openerp/server/openerp/osv/expression.py", line 371, in __init__
self.parse(cr, uid, distribute_not(normalize(exp)), table, context)
File "/opt/openerp/server/openerp/osv/expression.py", line 468, in parse
raise ValueError("Invalid field %r in domain expression %r" % (left, exp))
ValueError: Invalid field 'x_totalexp' in domain expression ['&', '&', ('x_totalexp', '=', 0), ['id', 'not in', []], ('name', 'ilike', 's')]
Is this a bug or i am doing it wrong
Its working now. I also faced the same problem but after "bzr pull", problem get solved.
Just update your 7.0 addons, web & server.