Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Validacions
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Immobiliari
    • Agència immobiliària
    • Estudi d'arquitectura
    • Construcció
    • Gestió immobiliària
    • Jardineria
    • Associació de propietaris de béns immobles
    Consultoria
    • Empresa comptable
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Mobles
    • Menjar
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Sistemes d'energia solar
    • Shoe Maker
    • Serveis de neteja
    • Instal·lacions HVAC
    Altres
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Comptabilitat
  • Inventari
  • PoS
  • Projectes
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

Best way to show json data on odoo UI

Subscriure's

Get notified when there's activity on this post

This question has been flagged
jsonodoo11
2 Respostes
31170 Vistes
Avatar
pawan

Hi, I have a model with a json field. I am trying to create a view for that model.
Approach 1: I want to parse the json field and then show that in UI. but I did not know how to achieve that.

Approach 2: Then I started with simply showing the json on UI. but it straight away gave an error.

my view is

<record id="view_farmpoint_tree" model="ir.ui.view">
<field name="name">fnf_geo.farmpoint.tree</field>
<field name="model">farmpoint</field>
<field name="arch" type="xml">
<tree string="Farm points">
<field name="coordinates"/>
<field name="is_success"/>
<field name="partner_id"/>
</tree>
</field>
</record>


my model is

class FarmPoint(models.Model):
_name = "farmpoint"
_description = "Stores coordinates of farms"

coordinates = fnf_fields.JSONB(string="Coordinates", required=True)
is_success = fields.Boolean(string="Successful data collection",
required=True, default=True)
partner_id = fields.Many2one('res.partner', string='Partner ID',
required=True, ondelete='restrict')


I was able to figure out that odoo does not support json as field, we have added custom field in odoo, and that's why it is not working.

I want to make it work as Approach 1, but any help is appreciated.


3
Avatar
Descartar
Avatar
Yenthe Van Ginneken (Mainframe Monkey)
Best Answer

Hi Pawan,

Why don't you just go with a plain text field (fields.Text) in which you store the JSON preformatted?
I assume that you get the JSON data from an API call so you can then format it with some Python code:

json.dumps(your_json_string, indent=4, sort_keys=True)

This will format and indent the JSON which you can then simply show in the views.

Regards,
Yenthe

7
Avatar
Descartar
pawan
Autor

Thanks for the answer @yenthe, that is how I solved it later.

Răzvan Anastasescu

This can work but is there a way to make use of the Postgres JSON field type which is very powerful in queries on JSON attributes and fields (not like a simple text column) ?

Yenthe Van Ginneken (Mainframe Monkey)

@Razvan it is possible but then you'd need to introduce a new field type into the core of Odoo as Odoo native doesn't support it.

Avatar
Dan Manastireanu
Best Answer

I also needed a json computed field on one of the models (a dict with some strings).







Creating a custom field type that can map to the json type in PostgreSQL is just the first step.



from odoo.fields import Field

import psycopg2



class JsonField(Field):

"""

Represents a postgresql Json column (JSON values are mapped to the Python equivalent type of list/dict).

"""

type = 'json' # Odoo type of the field (string)

column_type = ('json', 'json') # database column type (ident, spec)



def convert_to_column(self, value, record, values=None, validate=True):

""" Convert ``value`` from the ``write`` format to the SQL format. """

# By default the psycopg2 driver does not know how to map dict/list to postgresql json types

# We need to convert it to the right db type when inserting in the db

if value is None:

return None

else:

return \psycopg2\.extras\.Json\(value\)\\
\
\
\
\
\
\
\
Using\ it\ in\ the\ model\ is\ also\ easy\:\
\
\
\
from\ odoo\ import\ models\,\ fields\,\ api\
\
from\ json_field\ import\ JsonField\
\
\
\
class\ Person\(models\.Model\)\:\
\
\ \ \ \ _name\ \=\ \'example\.person\'\
\
\ \ \ \ _description\ \=\ \'Person\ with\ json\ details\'\
\
\ \ \ \ \
\
\ \ \ \ details\ \=\ JsonField\(\)\ \#\ a\ json\ object\ represented\ as\ dict\ \/\ list\ \/\ python\ primitives\
\
\
\
The\ hard\ part\ was\ getting\ the\ ui\ to\ recognize\ the\ field\ type\ to\ show\ and\ edit\ it\.\
\
\
\
I\ added\ all\ \ the\ code\ in\ this\ \ gist\ \ because\ \ it\'s\ \ a\ \ bit\ \ too\ \ long\ to\ write\ \ here\ \:\ \ \https://gist.github.com/danmana/5242f37b7d63daf4698de7c61c8b59fc#file-json_field-js



Note: There might be things I missed, I only needed it in a custom Kanban card (but the list view and edit seem to work fine).

It works ok as a computed field as well (or a normal field stored in db)




1
Avatar
Descartar
Henrik Norlin

Thank you very much for sharing this code! Would you like to publish it as an Odoo module in one of the repositories of https://github.com/oca ?

Dan Manastireanu

Hi Henrik,
I'm glad this helped (sorry for the bad formatting, I haven't figured out yet how to paste code on this forum).
Unfortunately at the moment I don't have any time to make a PR and properly test this code.
Feel free to copy the code and make a PR yourself :)

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registrar-se
Related Posts Respostes Vistes Activitat
How to return a simple json instead object JsonRpc? (Odoo11)
json jsonrpc odoo11
Avatar
Avatar
1
de set. 20
7235
create backorder odoo after validate purchase shipment ? Solved
odoo11
Avatar
Avatar
1
d’oct. 25
11874
I can't create any sub-task
odoo11
Avatar
Avatar
Avatar
2
de jul. 24
3576
odoo 11 display calendar widget in website layout
odoo11
Avatar
Avatar
1
de juny 24
5881
Get field's value from another model
odoo11
Avatar
Avatar
1
d’oct. 23
98
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now