问题
I am trying to create a new record in fleet.vehicle model. Since the Type of record is Many2One, it throws the following exception.
de.timroes.axmlrpc.XMLRPCServerException: Traceback (most recent call last):
However I was able to create a record in res.user model with the same code snippet because the Type was char for res.user model.
My code Snippet is:
public Integer create(String model, HashMap values) {
Integer newObjectId = null;
try {
XMLRPCClient client = new XMLRPCClient(mUrl);
Object[] parameters = {mDatabase, mUserId, mPassword, model, "create" ,new Object[]{values}};
newObjectId = (Integer) client.call("execute_kw", parameters);
} catch (XMLRPCException e) {
Log.d(CONNECTOR_NAME, e.toString());
}
return newObjectId;
}
and the log is:
de.timroes.axmlrpc.XMLRPCServerException: Traceback (most recent call last):
File "/home/wms/RMS/9040/new_odoo11/odoo/service/wsgi_server.py", line 124, in wsgi_xmlrpc
result = odoo.http.dispatch_rpc(service, method, params)
File "/home/wms/RMS/9040/new_odoo11/odoo/http.py", line 115, in dispatch_rpc
result = dispatch(method, params)
File "/home/wms/RMS/9040/new_odoo11/odoo/service/model.py", line 39, in dispatch
res = fn(db, uid, *params)
File "/home/wms/RMS/9040/new_odoo11/odoo/service/model.py", line 153, in execute_kw
return execute(db, uid, obj, method, *args, **kw or {})
File "/home/wms/RMS/9040/new_odoo11/odoo/service/model.py", line 97, in wrapper
return f(dbname, *args, **kwargs)
File "/home/wms/RMS/9040/new_odoo11/odoo/service/model.py", line 160, in execute
res = execute_cr(cr, uid, obj, method, *args, **kw)
File "/home/wms/RMS/9040/new_odoo11/odoo/service/model.py", line 149, in execute_cr
return odoo.api.call_kw(recs, method, args, kw)
File "/home/wms/RMS/9040/new_odoo11/odoo/api.py", line 687, in call_kw
return call_kw_model(method, model, args, kwargs)
File "/home/wms/RMS/9040/new_odoo11/odoo/api.py", line 672, in call_kw_model
result = method(recs, *args, **kwargs)
File "/home/wms/RMS/9040/new_odoo11/addons/fleet/models/fleet_vehicle.py", line 197, in create
vehicle = super(FleetVehicle, self.with_context(mail_create_nolog=True)).create(data)
File "/home/wms/RMS/9040/ac-ars/ac_rms/models/planner_calender.py", line 422, in create
res_id = super(MailThread, self).create(values)
File "/home/wms/RMS/9040/new_odoo11/addons/mail/models/mail_thread.py", line 231, in create
thread = super(MailThread, self).create(values)
File "/home/wms/RMS/9040/new_odoo11/odoo/models.py", line 3278, in create
record = self.browse(self._create(old_vals))
File "/home/wms/RMS/9040/new_odoo11/odoo/models.py", line 3371, in _create
cr.execute(query, tuple(u[2] for u in updates if len(u) > 2))
File "/home/wms/RMS/9040/new_odoo11/odoo/sql_db.py", line 155, in wrapper
return f(self, *args, **kwargs)
File "/home/wms/RMS/9040/new_odoo11/odoo/sql_db.py", line 232, in execute
res = self._obj.execute(query, params)
psycopg2.DataError: invalid input syntax for integer: "Audi A3"
LINE 1: ...et_vehicle_id_seq'), 5, true, 'kilometers', NULL, 'Audi A3',...
[1]
I have attached a image as well of what I am trying to create. Any help is much appreciated. My question is how to create a new record of type Many2One from Android/Java in odoo?
回答1:
I am answering my own question. The answer for this problem is very simple which I found out exploring, debugging and trying things out with XMLRPC library. This is how I solved it.
The field I was trying to update only accepts the data that is already preset in odoo. For example: I had to first search for the available preset data for that field by using search_read functionality. Like..
OdooConnect oc = OdooConnect.connect(SERVER_URL, PORT_NO, DB_NAME, USER_ID, PASSWORD);
List<HashMap<String, Object>> data = oc.search_read("fleet.vehicle", new Object[]{
new Object[]{condition}}, "id");
What I am doing here is searching for all the preset data related to the field I want to create and get the id of the string that matches with the one I want to update with certain conditions. here condition is
new Object[]{new Object[]{new Object[]{"name", "=", "Audi A3"}}}
so when it matches with that condition, it returns the id of the object I need to pass to the many2one field. In the above code "id" is for asking for an id of the string that matched the condition. Now we will pass the id while creating a new record to many2one field. Pass the id to below create method.
final Integer idC = oc.create("web.service", new HashMap() {{
put("name", "Final Test");
put("value", 29); //
put("partner_id", id); //many2One field. here we are using pre defined value i,e setting a value of other relation model to partner_id
}});
Hope it helps someone who is looking for an answer. :)
回答2:
From the traceback sycopg2.DataError: invalid input syntax for integer: "Audi A3" is saying you passing a non-integer value. Just replace Audi A3 with id. Then try again.
Hope it will help you.
来源:https://stackoverflow.com/questions/51496089/odoo-how-to-create-many2one-record-from-android-studio-using-axmlrpc-library