Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
4410 Lượt xem

Hello everyone, I'm using Odoo 15 and here's my controller code:


@http.route('/res_partner/create_partner', type='json', auth='none')
    def create_partner(self, json_data):

        vals = ast.literal_eval(json_data)
        admin_id = request.env.ref('base.user_admin')

        vals['company_id'] = admin_id.company_id.id
        vals['user_ids'] = [[6, False, [admin_id.id]]]
        vals['user_id'] = admin_id.id
        vals['is_company'] = False
        # vals['property_account_receivable_id'] = 2  
        # vals['property_account_payable_id'] = 3    
        vals['active'] = False
        vals['type'] = 'contact'
        vals['company_type'] = 'person'

        try:
            new_partner = request.env['res.partner'].sudo().create(vals)
        except Exception as ex:
            logging.warning(ex)
            data = {'response': [], 'message': "Exception occured while creating a partner"}
            return data
       
        else:
            if new_partner:
                data = {'response': [{'id': new_partner.id, 'name' : new_partner.name}], 'message': 'Success'}
            else:
                data = {'response': [], 'message': "Problem occurred with the new partner"}
            return data

Error:

2023-04-20 08:53:21,522 15886 ERROR odoo15_numerp odoo.sql_db: bad query:
                SELECT substr(p.res_id, 13)::integer, r.id
                FROM ir_property p
                LEFT JOIN stock_location r ON substr(p.value_reference, 16)::integer=r.id
                WHERE p.fields_id=3762
                    AND (p.company_id=false OR p.company_id IS NULL)
                    AND (p.res_id IN ('res.partner,56') OR p.res_id IS NULL)
                ORDER BY p.company_id NULLS FIRST
           
ERROR: operator does not exist: integer = boolean
LINE 6:                     AND (p.company_id=false OR p.company_id ...
                                             ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
 
2023-04-20 08:53:21,522 15886 WARNING odoo15_numerp root: operator does not exist: integer = boolean
LINE 6:                     AND (p.company_id=false OR p.company_id ...
                                             ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
 
2023-04-20 08:53:21,523 15886 INFO odoo15_numerp odoo.addons.phone_validation.tools.phone_validation: The `phonenumbers` Python module is not installed, contact numbers will not be verified. Please install the `phonenumbers` Python module.
2023-04-20 08:53:21,525 15886 ERROR odoo15_numerp odoo.sql_db: bad query: SELECT "res_users"."id" as "id", "res_users"."partner_id" as "partner_id", "res_users"."login" as "login", "res_users"."signature" as "signature", "res_users"."active" as "active", "res_users"."action_id" as "action_id", "res_users"."share" as "share", "res_users"."company_id" as "company_id", "res_users"."create_uid" as "create_uid", "res_users"."create_date" as "create_date", "res_users"."write_uid" as "write_uid", "res_users"."write_date" as "write_date", "res_users"."notification_type" as "notification_type", "res_users"."odoobot_state" as "odoobot_state", "res_users"."odoobot_failed" as "odoobot_failed", "res_users"."sale_team_id" as "sale_team_id", "res_users"."oauth_provider_id" as "oauth_provider_id", "res_users"."oauth_uid" as "oauth_uid", "res_users"."oauth_access_token" as "oauth_access_token" FROM "res_users" WHERE "res_users".id IN (1)
ERROR: current transaction is aborted, commands ignored until end of transaction block
 
2023-04-20 08:53:21,525 15886 ERROR odoo15_numerp odoo.http: Exception during JSON request handling.
Traceback (most recent call last):
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/api.py", line 886, in get
    return field_cache[record._ids[0]]
KeyError: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/fields.py", line 1057, in __get__
    value = env.cache.get(record, self)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/api.py", line 889, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: 'res.users(1,).partner_id'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/addons/base/models/ir_http.py", line 237, in _dispatch
    result = request.dispatch()
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/http.py", line 687, in dispatch
    result = self._call_function(**self.params)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/http.py", line 359, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/service/model.py", line 94, in wrapper
    return f(dbname, *args, **kwargs)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/http.py", line 355, in checked_call
    self._cr.flush()
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/sql_db.py", line 109, in flush
    self.transaction.flush()
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/api.py", line 833, in flush
    env_to_flush['base'].flush()
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/models.py", line 5644, in flush
    self.recompute()
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/models.py", line 6117, in recompute
    process(field)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/models.py", line 6101, in process
    field.recompute(recs)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/fields.py", line 1243, in recompute
    self.compute_value(recs)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/fields.py", line 1265, in compute_value
    records._compute_field_value(self)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/addons/mail/models/mail_thread.py", line 411, in _compute_field_value
    return super()._compute_field_value(field)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/models.py", line 4255, in _compute_field_value
    getattr(self, field.compute)()
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/addons/base/models/res_partner.py", line 301, in _compute_partner_share
    super_partner = self.env['res.users'].browse(SUPERUSER_ID).partner_id
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/fields.py", line 2603, in __get__
    return super().__get__(records, owner)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/fields.py", line 1083, in __get__
    recs._fetch_field(self)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/models.py", line 3276, in _fetch_field
    self._read(fnames)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/addons/base/models/res_users.py", line 449, in _read
    super(Users, self)._read(fields)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/models.py", line 3343, in _read
    cr.execute(query_str, params + [sub_ids])
  File "", line 2, in execute
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/sql_db.py", line 90, in check
    return f(self, *args, **kwargs)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/sql_db.py", line 313, in execute
    res = self._obj.execute(query, params)
Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/http.py", line 643, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/khishgee/Downloads/odoo15/odoo-15.0/NUM_ERP/odoo/http.py", line 301, in _handle_exception
    raise exception.with_traceback(None) from new_cause
psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

It seems that it is impossible to create a res.partner object when there's no session(not logged in). So, I found out that I need to authenticate as a user before calling the res.partner create method.


@http.route('/res_partner/create_partner', type='json', auth='none')
def create_partner(self, json_data):

​test = request.session.authenticate(db="odoo15", login="admin", password="admin")

​session_info = request.env['ir.http'].session_info()

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 8 25
2468
2
thg 6 25
865
3
thg 6 24
3647
2
thg 10 23
5773
3
thg 9 23
2612