跳至内容
菜单
此问题已终结
2 回复
2540 查看

Hi, 


I'm building a custom addon for Odoo and I'm wondering if it would be possible to get, within the controller, the informations of the current model. 


For example, would it be possible to get the ID of the res.parner object I'm on ? I know how to get that information in the model but I can't figure it out how to get that in the controller. 


Here is my code 


@http.route('/myaddon/api-call', auth='user', csrf=False )
  def calling(self, **kwargs):
    theId = # How to get the ID of the res partner object I'm on ??
    payload = {   "Ctxt": "xxxxxxxxxxxxx",
                    "ID": theId,
                    }
    req = urllib.request.Request(url='https://myapi', headers={
        "Content-Type":"application/json"}, data = json.dumps(payload).encode())
    reply = json.loads(urllib.request.urlopen(req).read().decode('UTF-8'))
    if reply.get("error"):
     print('Alert!!')
     raise Exception(reply["error"])
    return json.dumps(reply) 


All the code works great except how to get that Id. 


Do you know how to do it ?


Thanks !

形象
丢弃
最佳答案

Hi,

Try this:@http.route('/myaddon/api-call', auth='user', csrf=False)

    def calling(self, **kwargs):

        partner_id = int(kwargs.get('partner_id', 0))  # assuming 'partner_id' is passed in the URL

        if not partner_id:

            raise ValueError('Partner ID not provided.')


        payload = {

            "Ctxt": "xxxxxxxxxxxxx",

            "ID": partner_id,

        }

        req = urllib.request.Request(

            url='https://myapi',

            headers={"Content-Type": "application/json"},

            data=json.dumps(payload).encode()

        )

        reply = json.loads(urllib.request.urlopen(req).read().decode('UTF-8'))

        if reply.get("error"):

            print('Alert!!')

            raise Exception(reply["error"])

        return json.dumps(reply)


Hope it helps

形象
丢弃

def _get_basic_product_information_purchase(self, product_or_template, pricelist, combination, **kwargs):
basic_information = dict(
**product_or_template.read(['description_purchase', 'display_name'])[0]
)

if not product_or_template.is_product_variant:
basic_information['id'] = False
combination_name = combination._get_combination_name()
if combination_name:
basic_information.update(
display_name=f"{basic_information['display_name']} ({combination_name})"
)

price = product_or_template.standard_price

purchase_order_id = int(kwargs.get('purchase_order_id', 0))

if purchase_order_id:
# Fetch the purchase order
purchase_order = request.env['purchase.order'].browse(purchase_order_id)

if purchase_order and purchase_order.partner_id:
# Find the seller for the specific partner in the current purchase order
seller = product_or_template.seller_ids.filtered(lambda s: s.partner_id.id == purchase_order.partner_id.id)
if seller:
price = seller[0].price

return dict(
**basic_information,
price=price
)

hi there, i also try the same code that you provide but i didnt get the current id of purchase order. could you help me?

编写者 最佳答案

It does thanks!

形象
丢弃
相关帖文 回复 查看 活动
4
10月 21
37536
3
11月 24
29890
2
2月 23
3021
1
8月 22
7773
1
12月 21
2709