Odoo 13 Error compute_function on one2many field - python

I want to synchronize one part of my One2Many field in a different model with the right match in another model. I have an one2many field in project.project which includes a field emp_id(hr.employee) and role_id(project.roles). Now in the model account.analytic.line (2nd picture) there is already the right employee so I just want to compute the right role and display it in the right field. But as far as I am now I get singleton error, I know its cause of many objects and it expects ony one. But to be honest I dont know how to compute the correct role of the right project.
This is my One2Many field (project.project)
There are many projects and i need to get the correct one with the exactly correct timesheet.
Role field (last row) account.analytic.line
My compute function:
class RoleSync(models.Model):
_inherit = 'account.analytic.line'
#_inherits = {'project.project': 'list_id'}
role_field_id = fields.Many2one(string='Role', compute='_compute_role')
def _compute_role(self):
emp = []
# to get the one2many field with the correct project
list = self.env['project.project'].search(
[('name', '=', self.project_id.name)]).list_id
# to get the right timesheet for the correct project
timesheet = self.env['account.analytic.line'].search(
[('project_id.name', '=', self.name)])
# iterate through timesheet and get name of employee
for val in timesheet:
emp.append(val.name)
# nested loop for list with employee names and the one2many field
for a in emp:
for b in list:
# if the names are the same
if a.emp == b.emp_id.name:
# take the role and display it in the field
self.role_field_id = b.emp_id.role_id
The Error:
Error:
Odoo Server Error
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/api.py", line 745, in get
value = self._data[field][record._ids[0]]
KeyError: 18
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1004, in __get__
value = env.cache.get(record, self)
File "/usr/lib/python3/dist-packages/odoo/api.py", line 751, in get
raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('account.analytic.line(18,).role_field_id', None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/models.py", line 5101, in ensure_one
_id, = self._ids
ValueError: too many values to unpack (expected 1)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/http.py", line 624, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 310, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/odoo/tools/pycompat.py", line 14, in reraise
raise value
File "/usr/lib/python3/dist-packages/odoo/http.py", line 669, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 350, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/service/model.py", line 94, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 339, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 915, in __call__
return self.method(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 515, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1285, in search_read
return self.do_search_read(model, fields, offset, limit, domain, sort)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1304, in do_search_read
return Model.web_search_read(domain, fields, offset=offset, limit=limit, order=sort)
File "/usr/lib/python3/dist-packages/odoo/addons/web/models/models.py", line 39, in web_search_read
records = self.search_read(domain, fields, offset=offset, limit=limit, order=order)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 4951, in search_read
result = records.read(fields)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 2965, in read
vals[name] = convert(record[name], record, use_name_get)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 5731, in __getitem__
return self._fields[key].__get__(self, type(self))
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 2333, in __get__
return super().__get__(records, owner)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1028, in __get__
self.compute_value(recs)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1113, in compute_value
records._compute_field_value(self)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 4003, in _compute_field_value
getattr(self, field.compute)()
File "/opt/Odoo/Custom_Addon/project_addon/models/analytic_class.py", line 18, in _compute_role
[('project_id.name', '=', self.name)])
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 988, in __get__
record.ensure_one()
File "/usr/lib/python3/dist-packages/odoo/models.py", line 5104, in ensure_one
raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: account.analytic.line(18, 12, 9, 6)

Related

Unable to insert UUID as integer in primary key column of a table created in spanner database

I have model written in declarative base of SQL Alchemy.
Class Roles(Base):
__tablename__ = "roles"
__table_args__ = (
Index("roles_name", "name", unique=True),
)
id = Column(Integer, primary_key=True, default=get_uuid())
name = Column(String(10), nullable=False)
As you may have noticed I have set the default value of primary key column id to get_uuid().
def get_uuid():
pk = uuid.uuid4().int >> 64
return pk
The above method return UUID as integer of bit size 64 or less. This is because the column id of this table is set to int and spanner can hold up to 64 bit.
So now to insert a row in this table -
>>> role = Roles()
>>> role.name = "Admin"
>>> session.add(role)
>>> session.commit()
This resulted in following exception -
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/google/api_core/grpc_helpers.py", line 72, in error_remapped_callable
return callable_(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/grpc/_channel.py", line 946, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/usr/local/lib/python3.10/site-packages/grpc/_channel.py", line 849, in _end_unary_response_blocking
raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.FAILED_PRECONDITION
details = "Could not parse 18011687921562567628 as an integer"
debug_error_string = "UNKNOWN:Error received from peer ipv4:172.19.0.3:9010 {grpc_message:"Could not parse 18011687921562567628 as an integer", grpc_status:9, created_time:"2022-11-12T06:48:36.468914625+00:00"}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/cursor.py", line 269, in execute
) = self.connection.run_statement(statement)
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/connection.py", line 454, in run_statement
_execute_insert_heterogenous(
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/_helpers.py", line 57, in _execute_insert_heterogenous
transaction.execute_update(
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_v1/transaction.py", line 302, in execute_update
response = api.execute_sql(
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_v1/services/spanner/client.py", line 1096, in execute_sql
response = rpc(
File "/usr/local/lib/python3.10/site-packages/google/api_core/gapic_v1/method.py", line 154, in __call__
return wrapped_func(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/google/api_core/retry.py", line 283, in retry_wrapped_func
return retry_target(
File "/usr/local/lib/python3.10/site-packages/google/api_core/retry.py", line 190, in retry_target
return target()
File "/usr/local/lib/python3.10/site-packages/google/api_core/timeout.py", line 99, in func_with_timeout
return func(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/google/api_core/grpc_helpers.py", line 74, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.FailedPrecondition: 400 Could not parse 18011687921562567628 as an integer
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
self.dialect.do_execute(
File "/usr/local/lib/python3.10/site-packages/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py", line 1013, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/cursor.py", line 70, in wrapper
return function(cursor, *args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/cursor.py", line 289, in execute
raise IntegrityError(getattr(e, "details", e)) from e
google.cloud.spanner_dbapi.exceptions.IntegrityError: []
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 1451, in commit
self._transaction.commit(_to_root=self.future)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 829, in commit
self._prepare_impl()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 808, in _prepare_impl
self.session.flush()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 3386, in flush
self._flush(objects)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 3525, in _flush
with util.safe_reraise():
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
compat.raise_(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
raise exception
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 3486, in _flush
flush_context.execute()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/unitofwork.py", line 456, in execute
rec.execute(self)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/unitofwork.py", line 630, in execute
util.preloaded.orm_persistence.save_obj(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
_emit_insert_statements(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/persistence.py", line 1238, in _emit_insert_statements
result = connection._execute_20(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1705, in _execute_20
return meth(self, args_10style, kwargs_10style, execution_options)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", line 333, in _execute_on_connection
return connection._execute_clauseelement(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1572, in _execute_clauseelement
ret = self._execute_context(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1943, in _execute_context
self._handle_dbapi_exception(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2124, in _handle_dbapi_exception
util.raise_(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
raise exception
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
self.dialect.do_execute(
File "/usr/local/lib/python3.10/site-packages/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py", line 1013, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/cursor.py", line 70, in wrapper
return function(cursor, *args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/google/cloud/spanner_dbapi/cursor.py", line 289, in execute
raise IntegrityError(getattr(e, "details", e)) from e
sqlalchemy.exc.IntegrityError: (google.cloud.spanner_dbapi.exceptions.IntegrityError) []
[SQL: INSERT INTO roles (id, name) VALUES (%s, %s)]
[parameters: [18011687921562567628, 'Admin']]
(Background on this error at: https://sqlalche.me/e/14/gkpj)
What I understood for this is that the spanner is not willing to accept the generated UUID.
status = StatusCode.FAILED_PRECONDITION
details = "Could not parse 18011687921562567628 as an integer"
I have checked the method get_uuid(). It does return int value of but size 64 or less.
The README of this repo suggests creating a table's primary key as Integer and while in inserting a row in the database generate value of primary key in hex. I did exactly the same but it didn't work.
The generated int value is larger than the maximum INT64 value that is allowed in Cloud Spanner:
Max allowed: 9223372036854775807
Your value : 18011687921562567628
See https://cloud.google.com/spanner/docs/reference/standard-sql/data-types#integer_types for more information on the INT64 type.
I'm no Python expert, but my guess is that the int value that you are generating is interpreted as an unsigned int, while the INT64 data type in Cloud Spanner is signed.
EDIT: Add example to get signed value.
My understanding is that you can do the following to get a signed 64-bit integer value from a UUID in Python:
import ctypes
import uuid
ctypes.c_long(uuid.uuid4().int >> 64).value

how to fix External ID not found in the system on user create button?

I'm pretty new in Odoo, for the moment I dind't modified any code but this error is persistent when I try to create a new product.
Traceback (most recent call last):
File "/opt/odoo/odoo/odoo/tools/cache.py", line 85, in lookup
r = d[key]
File "/opt/odoo/odoo/odoo/tools/func.py", line 71, in wrapper
return func(self, *args, **kwargs)
File "/opt/odoo/odoo/odoo/tools/lru.py", line 34, in __getitem__
a = self.d[obj]
KeyError: ('product.template', <function ProductTemplate._get_default_category_id at 0x7f77854ab490>)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/odoo/odoo/odoo/tools/cache.py", line 85, in lookup
r = d[key]
File "/opt/odoo/odoo/odoo/tools/func.py", line 71, in wrapper
return func(self, *args, **kwargs)
File "/opt/odoo/odoo/odoo/tools/lru.py", line 34, in __getitem__
a = self.d[obj]
KeyError: ('ir.model.data', <function IrModelData._xmlid_lookup at 0x7f778a47b1c0>, 'product.product_category_all')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/odoo/odoo/odoo/addons/base/models/ir_http.py", line 237, in _dispatch
result = request.dispatch()
File "/opt/odoo/odoo/odoo/http.py", line 687, in dispatch
result = self._call_function(**self.params)
File "/opt/odoo/odoo/odoo/http.py", line 359, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/opt/odoo/odoo/odoo/service/model.py", line 94, in wrapper
return f(dbname, *args, **kwargs)
File "/opt/odoo/odoo/odoo/http.py", line 348, in checked_call
result = self.endpoint(*a, **kw)
File "/opt/odoo/odoo/odoo/http.py", line 916, in __call__
return self.method(*args, **kw)
File "/opt/odoo/odoo/odoo/http.py", line 535, in response_wrap
response = f(*args, **kw)
File "/opt/odoo/odoo/addons/web/controllers/main.py", line 1347, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/opt/odoo/odoo/addons/web/controllers/main.py", line 1339, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/opt/odoo/odoo/odoo/api.py", line 464, in call_kw
result = _call_kw_multi(method, model, args, kwargs)
File "/opt/odoo/odoo/odoo/api.py", line 451, in _call_kw_multi
result = method(recs, *args, **kwargs)
File "/opt/odoo/odoo/odoo/models.py", line 6356, in onchange
defaults = self.default_get(missing_names)
File "/opt/odoo/odoo/odoo/models.py", line 1410, in default_get
defaults[name] = field.default(self)
File "<decorator-gen-151>", line 2, in _get_default_category_id
File "/opt/odoo/odoo/odoo/tools/cache.py", line 90, in lookup
value = d[key] = self.method(*args, **kwargs)
File "/opt/odoo/odoo/addons/product/models/product_template.py", line 24, in _get_default_category_id
return self.env.ref('product.product_category_all')
File "/opt/odoo/odoo/odoo/api.py", line 578, in ref
res_model, res_id = self['ir.model.data']._xmlid_to_res_model_res_id(
File "/opt/odoo/odoo/odoo/addons/base/models/ir_model.py", line 1935, in _xmlid_to_res_model_res_id
return self._xmlid_lookup(xmlid)[1:3]
File "<decorator-gen-35>", line 2, in _xmlid_lookup
File "/opt/odoo/odoo/odoo/tools/cache.py", line 90, in lookup
value = d[key] = self.method(*args, **kwargs)
File "/opt/odoo/odoo/odoo/addons/base/models/ir_model.py", line 1928, in _xmlid_lookup
raise ValueError('External ID not found in the system: %s' % xmlid)
Exception
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/odoo/odoo/odoo/http.py", line 643, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/opt/odoo/odoo/odoo/http.py", line 301, in _handle_exception
raise exception.with_traceback(None) from new_cause
ValueError: External ID not found in the system: product.product_category_all
A little of backstory, I imported a lot of products, atributes and categories and did some testing deleting and importing again that is probably the origin of the issue.
The external ID product.product_category_all should be one of the default product categories introduced/installed by the app/module "product".
Obviously Odoo is using this one as a default value and you get an error, because it was deleted.
The easiest fix is to just update the app/module "product". Odoo will recreate the category and the error should be gone.

Unable fetch Products in TransientModel odoo 11

So far i had read many questions but i am sure that i am total a mix up because i am unable understand how to use Search at odoo TransientModel.
I had written a code that get Active_ids from self
context=self.env.context.get('active_ids')
I am supposing these actives_ids as
product_tmpl_id but when i tried to use them
product_recs = produtc_obj.search([('product_tmpl_id', 'in', context)])
print(product_recs)
result = {}
for rec in product_recs:
print(rec.default_code )
result[rec.default_code ]
but its always return
result[rec.default_code ]
KeyError: '1'
Here is my full code
import logging
from odoo import models, fields, api
from odoo.exceptions import Warning
_logger = logging.getLogger(__name__)
class product_export_to_rakuten(models.TransientModel):
_name = 'rakuten_ftp.export_product'
#api.multi
def export_products(self):
# check for more than one orders.
# print(self.env)
context=self.env.context.get('active_ids')
produtc_obj = self.env['product.product']
product_recs = produtc_obj.search([('product_tmpl_id', 'in', context)])
print(product_recs)
result = {}
for rec in product_recs:
print(rec.default_code )
result[rec.default_code ]
Here is the Error
Traceback (most recent call last):
File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 647, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 307, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
File "C:/Odoo_Source_Codes/odoo11\odoo\tools\pycompat.py", line 87, in reraise
raise value
File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 689, in dispatch
result = self._call_function(**self.params)
File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 339, in _call_function
return checked_call(self.db, *args, **kwargs)
File "C:/Odoo_Source_Codes/odoo11\odoo\service\model.py", line 97, in wrapper
return f(dbname, *args, **kwargs)
File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 332, in checked_call
result = self.endpoint(*a, **kw)
File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 933, in __call__
return self.method(*args, **kw)
File "C:/Odoo_Source_Codes/odoo11\odoo\http.py", line 512, in response_wrap
response = f(*args, **kw)
File "C:\Odoo_Source_Codes\odoo11\addons\web\controllers\main.py", line 934, in call_button
action = self._call_kw(model, method, args, {})
File "C:\Odoo_Source_Codes\odoo11\addons\web\controllers\main.py", line 922, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "C:/Odoo_Source_Codes/odoo11\odoo\api.py", line 689, in call_kw
return call_kw_multi(method, model, args, kwargs)
File "C:/Odoo_Source_Codes/odoo11\odoo\api.py", line 680, in call_kw_multi
result = method(recs, *args, **kwargs)
File "C:\Odoo_Source_Codes\odoo11\custom_addons\rakuten_ftp\wizard\export_product.py", line 22, in export_products
result[rec.default_code ]
KeyError: '1'
Everything seems to be fine with the Transient Model, the problem is that you're trying to read a dictionary value whose key doesn't exist yet. I mean, the default_code of the first product you get in the loop is 1, and you're telling Python: I want to read the value of the key 1 of the dictionary result, but this one is empty, so you get the error (you need to fill it in first).
You can reply the error in a Python console, this is what is happening to you:
>>> result = {}
>>> result['1']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: '1'
You should do something like this to make it work:
>>> result = {}
>>> result['1'] = 'Your value' # result.update({'1': 'Your value', })
>>> result['1']
'Your value'

SqlAlchemy: Could not locate column in row for column 'translate'

I have a problem with my endpoint in Falcon framework, I have two models Class with access to DB but when execute the seconds class, this raise an exception:
sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'translate'
I don't have a 'translate' column in my database or in my object mapping.
Version:
Python 3.6.2
PyMySQL==0.7.11
SQLAlchemy==1.1.11
My Code:
endpoint:
def on_post(self, req, resp, courseid, examid):
with self.db.connect() as cnn:
mdl_party = Party(cnn)
mdl_exam = Exam(cnn)
rolecourseid = mdl_party.find_rolecourseid(roleid,courseid,examid)
if rolecourseid is None :
raise AppValidationEx("ExamDoesntExist")
take_exam_id = mdl_exam.new_user_exam(username, rolecourseid, examid)
Class Party
class Party(object):
def __init__(self, dbconnection):
self.cnn = dbconnection
def find_rolecourseid(self, roleid, courseid, examid):
s = text(''' SELECT ... ''')
cursor = self.cnn.execute(s,
p_roleid=roleid,
p_courseid=courseid,
p_examid=examid)
return cursor.first()
Class Exam
class Exam(object):
""" a user class """
def __init__(self, dbconnection):
self.cnn = dbconnection
def new_user_exam(self, username, rolecourseid, examid):
now = datetime.utcnow()
data = {
'partyRoleCourseId': rolecourseid,
'examTakeDate': now,
'examTakeStart': now,
'examId': examid,
'createdBy': username,
'createdDate': now,
'deleted': 0,
'version': 0
}
ins = partyrolecourseexam.insert().values(data)
cursor = self.cnn.execute(ins) #this raise exception
take_exam_id = cursor.inserted_primary_key[0]
Database
engine = create_engine("mysql+pymysql://%s:%s#%s/%s?charset=utf8" % DBPARAMS,
pool_size=50,
max_overflow=100,
echo=False)
metadata = MetaData()
partyrolecourseexam = Table('partyrolecourseexam', metadata,
Column("id", Integer, primary_key=True),
Column("partyRoleCourseId", Integer),
...
)
The error raise when execute the "new_user_exam" method.
if the function 'find_rolecourseid' does not execute, the method "new_user_exam" works.
All trace:
Traceback (most recent call last):
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 73, in __getitem__
processor, obj, index = self._keymap[key]
KeyError: 'translate'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 99, in __getattr__
return self[name]
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 75, in __getitem__
processor, obj, index = self._parent._key_fallback(key)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 563, in _key_fallback
expression._string_or_unprintable(key))
sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'translate'"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\USER\dev\clexam\env\lib\site-packages\tornado\web.py", line 1488, in _execute
result = self.prepare()
File "C:\Users\USER\dev\clexam\env\lib\site-packages\tornado\web.py", line 2774, in prepare
self.fallback(self.request)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\tornado\wsgi.py", line 277, in __call__
WSGIContainer.environ(request), start_response)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\falcon\api.py", line 230, in __call__
if not self._handle_exception(ex, req, resp, params):
File "C:\Users\USER\dev\clexam\env\lib\site-packages\falcon\api.py", line 657, in _handle_exception
err_handler(ex, req, resp, params)
File "app_rest.py", line 17, in handle_errors
raise ex
File "C:\Users\USER\dev\clexam\env\lib\site-packages\falcon\api.py", line 227, in __call__
responder(req, resp, **params)
File "C:\Users\USER\dev\clexam\controllers\exam_take.py", line 32, in on_post
take_exam_id = mdl_exam.new_user_exam(username, rolecourseid, examid)
File "C:\Users\USER\dev\clexam\models\exam.py", line 43, in new_user_exam
cursor = self.cnn.execute(ins)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 945, in execute
return meth(self, multiparams, params)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\sql\elements.py", line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1053, in _execute_clauseelement
compiled_sql, distilled_params
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1189, in _execute_context
context)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1405, in _handle_dbapi_exception
util.reraise(*exc_info)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
context)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\default.py", line 470, in do_execute
cursor.execute(statement, parameters)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 164, in execute
query = self.mogrify(query, args)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 143, in mogrify
query = query % self._escape_args(args, conn)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 123, in _escape_args
return dict((key, conn.literal(val)) for (key, val) in args.items())
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\cursors.py", line 123, in <genexpr>
return dict((key, conn.literal(val)) for (key, val) in args.items())
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\connections.py", line 821, in literal
return self.escape(obj, self.encoders)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\connections.py", line 814, in escape
return escape_item(obj, self.charset, mapping=mapping)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\converters.py", line 27, in escape_item
val = encoder(val, mapping)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\converters.py", line 110, in escape_unicode
return u"'%s'" % _escape_unicode(value)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\pymysql\converters.py", line 73, in _escape_unicode
return value.translate(_escape_table)
File "C:\Users\USER\dev\clexam\env\lib\site-packages\sqlalchemy\engine\result.py", line 101, in __getattr__
raise AttributeError(e.args[0])
AttributeError: Could not locate column in row for column 'translate'
Help and thks

TypeError: int() argument must be a string or a number, not 'dict'

I'm try to get all the building structures with the hazard type "High". I have this following query:
>>> reference = FloodHazard.objects.filter(hazard='High')
>>> ids = reference.values('id')
>>> for id in ids:
... getgeom = FloodHazard.objects.get(id=id).geom
... getbldg = BuildingStructure.objects.filter(geom__intersects=getgeom).value_list('id')
Traceback (most recent call last):
File "<console>", line 3, in <module>
File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 151, in get
return self.get_queryset().get(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 298, in get
clone = self.filter(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 590, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 608, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1198, in add_q
clause = self._add_q(where_part, used_aliases)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1234, in _add_q
current_negated=current_negated)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1125, in build_filter
clause.add(constraint, AND)
File "C:\Python27\lib\site-packages\django\utils\tree.py", line 104, in add
data = self._prepare_data(data)
File "C:\Python27\lib\site-packages\django\contrib\gis\db\models\sql\where.py", line 42, in _prepare_data
return super(GeoWhereNode, self)._prepare_data(data)
File "C:\Python27\lib\site-packages\django\db\models\sql\where.py", line 79, in _prepare_data
value = obj.prepare(lookup_type, value)
File "C:\Python27\lib\site-packages\django\db\models\sql\where.py", line 352, in prepare
return self.field.get_prep_lookup(lookup_type, value)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 369, in get_prep_lookup
return self.get_prep_value(value)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 613, in get_prep_value
return int(value)
TypeError: int() argument must be a string or a number, not 'dict'
So, how do I get the IDs of all the FloodHazard with hazard "high"? I understand error but when I try reference.id it returns AttributeError: 'GeoQuerySet' object has no attribute 'id'.
values() returns a list of dicts. You should use the values_list() method instead:
ids = reference.values_list('id', flat=True)
Use:
evntids=EventMember.objects.values('event_id').distinct()
output in dictionary form:
QuerySet [{'event_id': 1}, {'event_id': 2}]
Use:
evntids=EventMember.objects.values_list('event_id',flat=True).distinct()
output in int form:
QuerySet [1, 2]

Categories