Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
2525 Vistas

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 !

Avatar
Descartar
Mejor respuesta

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

Avatar
Descartar

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?

Autor Mejor respuesta

It does thanks!

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
4
oct 21
37506
3
nov 24
29822
2
feb 23
3009
1
ago 22
7762
1
dic 21
2698