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

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 !

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

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

Ảnh đại diện
Huỷ bỏ

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?

Tác giả Câu trả lời hay nhất

It does thanks!

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
4
thg 10 21
37558
3
thg 11 24
29958
2
thg 2 23
3069
1
thg 8 22
7818
1
thg 12 21
2724