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

Hey!

I've read this post: https://www.odoo.com/es_ES/forum/ayuda-1/post-to-external-url-from-server-action-99309 and asked for some help there but got none, also I cannot continue the conversation there since i don't have enough karma. My question is how can i do an external HTTP request on a Server Action?, I've tried with Python Requests like:

```
requests.get("http://example.com")
```

but i get:

ValueError: <class 'NameError'>: "name 'requests' is not defined" while evaluating

I've tried installing urllib3 with: `sudo pip install urllib3` but looks like it is already installed:

`Requirement already satisfied: urllib3 in /opt/bitnami/python/lib/python3.7/site-packages/urllib3-1.22-py3.7.egg`

thanks!

形象
丢弃
最佳答案

Hello,

In server actions I don't believe this is possible, when using requests you need to import the requests python library.

I have found the best way to achieve what you need is to create a custom module with a model that extends the model you want the server action to run on and define a function. Eg Def call_endpoint(self):

Then you can call this function in the server action you call this function using record.call_endpoint() and you request call will be in this function.

If you need further help, let me know,

Thanks,

形象
丢弃
最佳答案

Hello,

I have the same problem, can someone help me please?

Thanks 

形象
丢弃
最佳答案

The only Python libraries you can access in a Server Action are: datetime, dateutil, time, timezone.


See https://www.odoo.com/documentation/14.0/reference/actions.html#evaluation-context - to use other libraries, write a module that creates an action that you can then call, review the base_geolocalize module for an idea (this module leverages Open StreetMap)

形象
丢弃
编写者 最佳答案

Thanks @Jack Dane, i did what you suggested and it worked!

形象
丢弃
相关帖文 回复 查看 活动
3
8月 20
10127
0
7月 17
2998
2
8月 20
7787
2
8月 24
1172
0
1月 25
1010