OCA / odoorpc

Python module to pilot your Odoo servers through JSON-RPC.
http://pythonhosted.org/OdooRPC/
GNU Lesser General Public License v3.0
237 stars 125 forks source link

Expected Singleton error #46

Closed swscomputers closed 5 years ago

swscomputers commented 5 years ago

Hello all, I'm using OdooRpc to pull some data from the products template. Expected behavior: I used to be able to do this...

def pricelist(): odoo = get_session() p = odoo.env['product.template']

product_ids = p.search([('sale_ok', '=', True)])
products = p.browse(product_ids)

csv = ""
for p in products:
    csv += str(p.categ_id.name) + "," + str(p.name) + "," + str(p.list_price) + "," + str(p.default_code) + "," + str(p.item_ids.fixed_price) + "," + str(p.qty_available) + "\n"

f = open("pricelist-auto.txt", "w")
f.write(csv)

This has stopped working. I'm using Odoo 11 Enterprise. This is the error I get odoorpc.error.RPCError: Expected singleton: product.template(132,...)

Next attempt: This is terribly inefficient but it worked a couple times...

def pricelist_2(): odoo = get_session() p = odoo.env['product.template']

product_ids = p.search([('sale_ok', '=', True)])

csv = ""
for pro in product_ids:
    p = odoo.env['product.template'].browse(pro)
    csv += str(p.categ_id.name) + "," + str(p.name) + "," + str(p.list_price) + "," + str(p.default_code) + "," + str(p.item_ids.fixed_price) + "," + str(p.qty_available) + "\n"

f = open("pricelist-auto.txt", "w")
f.write(csv)

This take 20 minutes because there are like 2000 products. It still come up with the same error. Mind you the first piece of code was working perfectly fine.

If there I fix for this or am I doing something wrong?

Thanks for any help you can provide.

sebalix commented 5 years ago

Hi @swscomputers Can you provide the full traceback of the script? (it will help to show which code line generates the error) Did you upgrade OdooRPC or the Odoo server before the error occurs?

EDIT: if you have access to the log of the server it also will help (especially the traceback generated server-side)

bodedra commented 5 years ago

It seems to me issue on following line

str(p.item_ids.fixed_price)

In p.item_ids will have more than one recordset.

Solution:

swscomputers commented 5 years ago

Thank you guys I will get the traceback from the server. I will also try bodedra solution to see if that might help.

pedrobaeza commented 5 years ago

This has nothing to do with odoorpc, so closing.

swscomputers commented 5 years ago

Here is the traceback I get from the terminal. This is using the first script with bodedra's suggestion.

Traceback (most recent call last): File "./all_product_prices.py", line 130, in main() File "./all_product_prices.py", line 125, in main pricelist() File "./all_product_prices.py", line 91, in pricelist products = p.browse(product_ids) File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/odoorpc/models.py", line 280, in browse return cls._browse(cls.env, ids) File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/odoorpc/models.py", line 246, in _browse records._init_values() File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/odoorpc/models.py", line 361, in _init_values self.ids, basic_fields, context=context, load='_classic_write') File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/odoorpc/models.py", line 74, in rpc_method cls._name, method, args, kwargs) File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/odoorpc/odoo.py", line 486, in execute_kw 'args': args_to_send}) File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/odoorpc/odoo.py", line 285, in json data['error']) odoorpc.error.RPCError: Expected singleton: product.template(5704768, 1, 3, 4, 5, 6, 7, 3257, 9, 11, 13, 14, 16, 19, 20, 21, 32790, 32791, 24, 25, 22, 27, 32796, 32797, 30, 31, 32799, 32801, 34, 35, 36, 37, 38, 39, 40, 41, 32807, 32806, 32808, 42, 43, 47, 48, 49, 3266, 52, 53, 3267, 56, 57, 58, 59, 60, 61, 62, 63, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 79, 80, 81, 82, 4014163, 4014162, 86, 87, 3326041, 91, 92, 94, 97, 98, 99, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 23, 116, 118, 119, 117, 121, 122, 123, 120, 65655, 65656, 126, 65660, 65661, 65666, 65667, 132, 26, 134, 135, 136, 137, 138, 139, 140, 28, 142, 4776079, 143, 4776081, 4776082, 4776083, 145, 146, 149, 29, 150, 147, 148, 155, 156, 157, 158, 159, 160, 161, 32, 162, 164, 165, 166, 33, 167, 169, 170, 171, 172, 173, 174, 3263, 176, 98481, 178, 179, 180, 181, 182, 183, 184, 185, 186, 98491, 98492, 98493, 98494, 191, 192, 98497, 98498, 98499, 98500, 196, 198, 98503, 195, 197, 199, 98507, 98508, 98509, 98510, 98511, 204, 98504, 208, 209, 98516, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 222, 221, 224, 226, 98530, 98532, 229, 98533, 231, 98534, 233, 234, 235, 236, 237, 238, 239, 240, 232, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 3307, 258, 259, 260, 261, 262, 263, 257, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 4301079, 3312, 284, 285, 286, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 2261294, 303, 304, 302, 305, 306, 307, 309, 310, 311, 312, 313, 314, 315, 316, 308, 318, 317, 319, 321, 322, 323, 320, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 342, 343, 344, 345, 346, 3325, 347, 349, 3326, 350, 353, 354, 355, 356, 357, 358, 359, 360, 361, 3327, 363, 362, 364, 366, 3329, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 3330, 380, 381, 382, 383, 385, 386, 387, 388, 390, 391, 392, 393, 394, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 438, 3670455, 3670456, 3670457, 440, 439, 441, 443, 5251518, 5251519, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 459, 460, 461, 462, 463, 464, 466, 468, 469, 470, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 488, 489, 490, 491, 492, 493, 496, 497, 498, 499, 500, 501, 502, 503, 504, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 520, 521, 522, 523, 524, 526, 527, 528, 529, 530, 533, 534, 539, 540, 541, 542, 543, 544, 545, 546, 548, 551, 552, 553, 555, 556, 557, 559, 560, 561, 564, 566, 5481017, 570, 571, 572, 575, 577, 578, 579, 580, 581, 582, 583, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 600, 603, 604, 605, 608, 610, 612, 614, 615, 616, 617, 618, 619, 620, 622, 623, 124, 625, 626, 627, 628, 631, 633, 634, 635, 636, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 686, 687, 688, 690, 691, 692, 693, 694, 695, 698, 699, 702, 704, 705, 710, 141, 716, 717, 720, 721, 722, 723, 724, 144, 733, 735, 739, 740, 741, 744, 745, 747, 3588843, 3588844, 758, 761, 765, 766, 767, 152, 769, 770, 771, 153, 773, 774, 775, 776, 154, 779, 780, 786, 794, 795, 796, 797, 798, 801, 807, 808, 816, 819, 163, 836, 840, 841, 842, 843, 168, 4055887, 849, 850, 851, 854, 860, 870, 872, 873, 874, 875, 876, 881, 883, 884, 885, 889, 891, 903, 905, 908, 934, 935, 940, 187, 943, 188, 947, 948, 189, 955, 190, 957, 958, 959, 962, 3277768, 970, 193, 974, 975, 976, 194, 977, 981, 982, 987, 58335, 991, 992, 996, 997, 998, 999, 1004, 1005, 201, 1017, 1021, 203, 1023, 1022, 1032, 205, 1037, 1038, 206, 1042, 207, 1051, 1055, 1056, 1060, 1061, 1062, 1063, 1065, 1066, 1068, 1070, 1071, 1072, 1075, 1077, 1078, 1083, 1085, 1092, 1096, 2655308, 2655309, 1102, 2655310, 3300, 1114, 1115, 1118, 1119, 1120, 1121, 223, 1122, 1124, 1127, 1128, 3301, 1130, 1132, 225, 1140, 227, 1145, 1146, 1147, 228, 32792, 1150, 1151, 1152, 1153, 1156, 1157, 1158, 230, 1162, 1165, 1166, 1173, 3384471, 1178, 1181, 1183, 1184, 32800, 1188, 1191, 1192, 1193, 1194, 1213, 1214, 241, 1218, 1369296, 1233, 1235, 1240, 124123, 1247, 1248, 1249, 1250, 1251, 124132, 1261, 83184, 1271, 1275, 1276, 1277, 1283, 1284, 2434318, 1301, 1302, 1313, 1319, 1324, 264, 1346, 3310, 1363, 5014881, 5014882, 1381, 1382, 1383, 1384, 1385, 1387, 1390, 1391, 1394, 1398, 1399, 1400, 1401, 1414, 17808, 1828252, 1469, 1470, 1476, 1493, 1496, 4105690, 4105691, 1503, 1504, 1505, 1506, 1507, 3317, 1531, 1534, 1541, 1543, 1546, 1549, 3318, 1557, 1562, 1567, 1570, 1579, 1585, 1595, 58963, 324, 1648, 1653, 1659, 1665, 1666, 1667, 1668, 1669, 1670, 3352201, 1678, 1679, 1683, 1684, 1685, 5080726, 1687, 1706, 1707, 1710, 1711, 1712, 1721, 3106493, 3106494, 1727, 3106495, 1742, 1743, 1750, 1755, 1758, 1765, 1766, 1770, 1772, 1773, 1779, 1780, 1794, 1797, 1798, 1802, 1806, 1809, 3328, 5359384, 1825, 18215, 1833, 1835, 18221, 18222, 18223, 18224, 18225, 18226, 18227, 18228, 18229, 18230, 18231, 18232, 18233, 18243, 18244, 18245, 18246, 18250, 1871, 1876, 1890, 51043, 51045, 4212582, 1893, 51047, 1901, 1904, 1911, 1914, 1915, 1916, 1917, 1930, 1937, 1939, 1950, 1955, 1956, 1957, 1959, 1976, 1978, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995)

swscomputers commented 5 years ago

what do you mean it has nothing do to with it? It was working before? with Odoo 11.

pedrobaeza commented 5 years ago

No, it was working by accident, but the problem is you are accessing a data for multiple records. This is something of Odoo itself, and not an issue, but a misuse of the ORM. The same will happen with direct Odoo code.

swscomputers commented 5 years ago

Listen I'm prone to make mistakes. I don't doubt I have an error somewhere. But please don't assume I'm ignorant either.

def get_report_values(self, docids, data=None):

    lines_per_page = 25 if data['form']['lines_per_page'] == 0 else data['form']['lines_per_page']
    products = self.env['product.template'].search([('categ_id.nrg_pricelist', '=', True), '!', ('nrg_pricelist','=', True)], order='categ_id asc, list_price asc')

    index = 0
    per_side_number = lines_per_page
    total_num_in_pages = lines_per_page * 2
    leftBool = True

    left = []
    right = []
    pages = []

    for product in products:
        if(leftBool):
            left.append({
                'name': product.name,
                'categ_name': product.categ_id.name,
                'list_price': product.list_price,
                'default_code': product.default_code,
            })
        else:
            right.append({
                'name': product.name,
                'categ_name': product.categ_id.name,
                'list_price': product.list_price,
                'default_code': product.default_code,
            })
        if( (index + 1) % total_num_in_pages == 0):
            pages.append({
                'left': list(left),
                'right': list(right)
            })
            left[:] = []
            right[:] = []
        if (index + 1 == len(products)):
            pages.append({
                'left': list(left),
                'right': list(right)
            })
            left[:] = []
            right[:] = []
        if ( (index + 1) % per_side_number == 0):
            leftBool = not leftBool

        index = index + 1

    return {
        'docs': products,
        'pages': pages,
    }

This is from Odoo itself and it works perfectly fine. I have similar code in multiple places. Again maybe your not wrong. But it's not like It's "misuse the ORM". You come off as real high and mighty.

swscomputers commented 5 years ago

Anyways thanks all for the help. Off to stackoverflow perhaps.

pedrobaeza commented 5 years ago

I'm not assuming you are ignorant, but we should concentrate on real OdooRPC problems here in the issue tracker.

bodedra commented 5 years ago

Issue is not with OdooRPC. I am agree with @pedrobaeza

Issue on these line:

File "./all_product_prices.py", line 91, in pricelist
products = p.browse(product_ids)

It seems you have already get browse recordset in product_ids variable.

Solution:

If you found real problem with OdooRPC, you are most welcome to report it.

IMO You may get help from Odoo forums.

swscomputers commented 5 years ago

Thanks for the help. That didn't work. Ill check the Odoo forums.

sebalix commented 5 years ago

@swscomputers this error seems to happen when OdooRPC try to read all your product_ids, this SingletonError can raise because of a (for instance) a compute field which doesn't loop on self (this kind of error may stay silent for a long time because it's working well when the field is displayed on a form, but will raise an error if you put it on a tree view (like here where OdooRPC try to read ALL the fields for multiple products, so it could be any code related to them, especially computed field).

EDIT: this call should raise the same error if I'm not wrong:

odoo.env['product.template'].read(product_ids, [])
vcmanzo commented 4 years ago

I know this is an old thread... But I just want to confirm @sebalix is right. The problem is due to the RPC/ORM trying to read all the fields in the record. If you specify the not calculated fields you should have no error. It just happened to me on the pos_order table and this was the answer.

@swscomputers this error seems to happen when OdooRPC try to read all your product_ids, this SingletonError can raise because of a (for instance) a compute field which doesn't loop on self (this kind of error may stay silent for a long time because it's working well when the field is displayed on a form, but will raise an error if you put it on a tree view (like here where OdooRPC try to read ALL the fields for multiple products, so it could be any code related to them, especially computed field).

EDIT: this call should raise the same error if I'm not wrong:

odoo.env['product.template'].read(product_ids, [])
sebalix commented 4 years ago

Thank you @vcmanzo to confirm this.