Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
14334 มุมมอง

Hey i am trying to build a rest api for mobile application and getting employee detail in it. When i get image_1920 in json it outputs a very long array and i need URL instead of that . Can anyone help me with it? here is my code

Hremployee = request.env['hr.employee']
employee_rec = Hremployee.sudo().search([('user_id', '=', user.id)])
employee = []
for rec in employee_rec:
vals = {
'id': rec.id,
'employee_name': rec.name,
'image': rec.image_1920,
'badge': rec.barcode,

}
employee.append(vals)

records = {'status': 200, 'response': employee, 'message': 'All Employee Details fetched'}
return records
อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hello Abdullah,

To get URL instead of json output You need to create Image Url. Please find below code to create Emloyee's inage as url.

base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')

image_url_1920 = base_url + '/web/image?' + 'model=hr_employees&id=' + str(rec.id) + '&field=image_1920'

Thank you!

Regards,




Email:      odoo@aktivsoftware.com  

Skype: kalpeshmaheshwari

   

อวตาร
ละทิ้ง
ผู้เขียน

Thanks for replying jainesh. Let me try and get back to you

ผู้เขียน
ผู้เขียน คำตอบที่ดีที่สุด

Jainsh answer is almost correct. What worked for me at localhost is this

base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')

image_url_1920 = base_url + '/web/image?' + 'model=hr.employee&id=' + str(rec.id) + '&field=image_128'
อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Odoo.sh 15...

This is for Product Variants, but same concept.

In Studio, I made a new text field called "Image URL" (which auto-gets a technical name of x_studio_image_url​).


Then, since I still don't know after 2+ years how to make Stored values work like they're supposed to in Odoo.sh, I created an (inactive) Scheduled Action that I run manually to set all the fields' values:

base_url = env['ir.config_parameter'].sudo().get_param('web.base.url')
products = env['product.product'].search([])
query = """
  UPDATE product_product
  SET x_studio_image_url = %s
  WHERE id = %s
"""

for record in products:
image_url_1920 = base_url + '/web/image?model=product.product&id=' + str(record.id) + '&field=image_1920'
env.cr.execute(query, (image_url_1920, record.id))


Here's the code you'd put in the custom field's code if I ever learned how to make "Stored" fields work. It works if the custom field is not "Stored", but as soon as you make it "Stored" the field goes blank. (It probably needs to be "Stored" in order to use in an API or export.) I just set the required "Dependency" field to something random like "name".

for record in self:
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
image_url_1920 = base_url + '/web/image?' + 'model=product.product&id=' + str(record.id) + '&field=image_1920'
record['x_studio_imageurl_text'] = image_url_1920


(If somebody could enlighten me on how to make Stored Odoo fields that don't show as blank values, that'd be sweet too...)

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
5
ก.ย. 22
18442
2
ก.ค. 22
11730
2
เม.ย. 17
10547
0
พ.ย. 23
1734
2
ก.ค. 23
3269