Перейти к содержимому
Меню
Чтобы взаимодействовать с сообществом, необходимо зарегистрироваться.
Этот вопрос был отмечен
2 Ответы
2233 Представления

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!

Аватар
Отменить
Related Posts Ответы Просмотры Активность
4
окт. 21
37196
3
нояб. 24
29336
2
февр. 23
2762
1
авг. 22
7510
1
дек. 21
2448