Mari kita susun struktur MVC untuk menampilkan data dari URL "https://dummyjson.com/products" dengan format JSON :
Model (models.py):
python
Copy code
from odoo import models, fields, api
class Product(models.Model):
_name = 'your_module.product'
_description = 'Product Model'
title = fields.Char(string='Title')
description = fields.Text(string='Description')
price = fields.Float(string='Price')
discount_percentage = fields.Float(string='Discount Percentage')
rating = fields.Float(string='Rating')
stock = fields.Integer(string='Stock')
brand = fields.Char(string='Brand')
category = fields.Char(string='Category')
thumbnail = fields.Char(string='Thumbnail URL')
images = fields.Many2many('ir.attachment', 'product_images_rel', 'product_id', 'image_id', string='Images')
View (views.xml):
xml
Copy code
<record id="view_product_form" model="ir.ui.view">
<field name="name">Product Form</field>
<field name="model">your_module.product</field>
<field name="arch" type="xml">
<form>
<group>
<field name="title"/>
<field name="description"/>
<field name="price"/>
<field name="discount_percentage"/>
<field name="rating"/>
<field name="stock"/>
<field name="brand"/>
<field name="category"/>
<field name="thumbnail"/>
<field name="images" widget="many2many_tags" options="{'no_create': True}" />
</group>
</form>
</field>
</record>
Controller (controllers.py):
python
Copy code
from odoo import http
from odoo.http import request
import requests
class ProductController(http.Controller):
@http.route('/your_module/fetch_data', type='json', auth='user')
def fetch_data(self):
url = 'https://dummyjson.com/products'
response = requests.get(url)
data = response.json().get('products', [])
product_model = request.env['your_module.product']
for product_data in data:
product_model.create({
'title': product_data.get('title'),
'description': product_data.get('description'),
'price': product_data.get('price'),
'discount_percentage': product_data.get('discountPercentage'),
'rating': product_data.get('rating'),
'stock': product_data.get('stock'),
'brand': product_data.get('brand'),
'category': product_data.get('category'),
'thumbnail': product_data.get('thumbnail'),
'images': [(0, 0, {'name': f'Image {i}', 'url': image_url}) for i, image_url in enumerate(product_data.get('images', []))]
})
return {'status': 'success'}
Menu (views.xml):
xml
Copy code
<record id="menu_fetch_data" model="ir.ui.menu">
<field name="name">Fetch Data</field>
<field name="action" ref="your_module.action_fetch_data"/>
</record>
Tombol Aksi (views.xml):
xml
Copy code
<record id="action_fetch_data" model="ir.actions.server">
<field name="name">Fetch Data from API</field>
<field name="model_id" ref="your_module.model_your_model"/>
<field name="code">controller.fetch_data()</field>
</record>
Pastikan Anda menyesuaikan kode ini dengan kebutuhan spesifik Anda dan memastikan bahwa Anda telah membuat modul Odoo dengan benar serta menanggapi permintaan HTTP dengan mengatur endpoint URL dengan benar.