Skip to Content
Odoo Menu
  • Log ind
  • Prøv gratis
  • Apps
    Økonomi
    • Bogføring
    • Fakturering
    • Udgifter
    • Regneark (BI)
    • Dokumenter
    • e-Signatur
    Salg
    • CRM
    • Salg
    • POS Butik
    • POS Restaurant
    • Abonnementer
    • Udlejning
    Hjemmeside
    • Hjemmesidebygger
    • e-Handel
    • Blog
    • Forum
    • LiveChat
    • e-Læring
    Forsyningskæde
    • Lagerbeholdning
    • Produktion
    • PLM
    • Indkøb
    • Vedligeholdelse
    • Kvalitet
    HR
    • Medarbejdere
    • Rekruttering
    • Fravær
    • Medarbejdersamtaler
    • Anbefalinger
    • Flåde
    Marketing
    • Markedsføring på sociale medier
    • E-mailmarketing
    • SMS-marketing
    • Arrangementer
    • Automatiseret marketing
    • Spørgeundersøgelser
    Tjenester
    • Projekt
    • Timesedler
    • Udkørende Service
    • Kundeservice
    • Planlægning
    • Aftaler
    Produktivitet
    • Dialog
    • Godkendelser
    • IoT
    • VoIP
    • Vidensdeling
    • WhatsApp
    Tredjepartsapps Odoo Studio Odoo Cloud-platform
  • Brancher
    Detailhandel
    • Boghandel
    • Tøjforretning
    • Møbelforretning
    • Dagligvarebutik
    • Byggemarked
    • Legetøjsforretning
    Mad og værtsskab
    • Bar og pub
    • Restaurant
    • Fastfood
    • Gæstehus
    • Drikkevareforhandler
    • Hotel
    Ejendom
    • Ejendomsmægler
    • Arkitektfirma
    • Byggeri
    • Ejendomsadministration
    • Havearbejde
    • Boligejerforening
    Rådgivning
    • Regnskabsfirma
    • Odoo-partner
    • Marketingbureau
    • Advokatfirma
    • Rekruttering
    • Audit & certificering
    Produktion
    • Tekstil
    • Metal
    • Møbler
    • Fødevareproduktion
    • Bryggeri
    • Firmagave
    Heldbred & Fitness
    • Sportsklub
    • Optiker
    • Fitnesscenter
    • Kosmetolog
    • Apotek
    • Frisør
    Håndværk
    • Handyman
    • IT-hardware og support
    • Solenergisystemer
    • Skomager
    • Rengøringsservicer
    • VVS- og ventilationsservice
    Andet
    • Nonprofitorganisation
    • Miljøagentur
    • Udlejning af billboards
    • Fotografi
    • Cykeludlejning
    • Softwareforhandler
    Gennemse alle brancher
  • Community
    Få mere at vide
    • Tutorials
    • Dokumentation
    • Certificeringer
    • Oplæring
    • Blog
    • Podcast
    Bliv klogere
    • Udannelselsesprogram
    • Scale Up!-virksomhedsspillet
    • Besøg Odoo
    Få softwaren
    • Download
    • Sammenlign versioner
    • Udgaver
    Samarbejde
    • Github
    • Forum
    • Arrangementer
    • Oversættelser
    • Bliv partner
    • Tjenester til partnere
    • Registrér dit regnskabsfirma
    Modtag tjenester
    • Find en partner
    • Find en bogholder
    • Kontakt en rådgiver
    • Implementeringstjenester
    • Kundereferencer
    • Support
    • Opgraderinger
    Github Youtube Twitter LinkedIn Instagram Facebook Spotify
    +1 (650) 691-3277
    Få en demo
  • Prissætning
  • Hjælp

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

  • CRM
  • e-Commerce
  • Bogføring
  • Lager
  • PoS
  • Projekt
  • MRP
All apps
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Hjælp

Best way to show json data on odoo UI

Tilmeld

Få besked, når der er aktivitet på dette indlæg

Dette spørgsmål er blevet anmeldt
jsonodoo11
2 Besvarelser
31129 Visninger
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
Kassér
Avatar
Yenthe Van Ginneken (Mainframe Monkey)
Bedste svar

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
Kassér
pawan
Forfatter

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
Bedste svar

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
Kassér
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!

Tilmeld dig
Related Posts Besvarelser Visninger Aktivitet
How to return a simple json instead object JsonRpc? (Odoo11)
json jsonrpc odoo11
Avatar
Avatar
1
sep. 20
7233
create backorder odoo after validate purchase shipment ? Løst
odoo11
Avatar
Avatar
1
okt. 25
11864
I can't create any sub-task
odoo11
Avatar
Avatar
Avatar
2
jul. 24
3552
odoo 11 display calendar widget in website layout
odoo11
Avatar
Avatar
1
jun. 24
5876
Get field's value from another model
odoo11
Avatar
Avatar
1
okt. 23
98
Community
  • Tutorials
  • Dokumentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Oversættelser
Tjenester
  • Odoo.sh-hosting
  • Support
  • Opgradere
  • Individuelt tilpasset udvikling
  • Uddannelse
  • Find en bogholder
  • Find en partner
  • Bliv partner
Om os
  • Vores virksomhed
  • Brandaktiver
  • Kontakt os
  • Stillinger
  • Arrangementer
  • Podcast
  • Blog
  • Kunder
  • Juridiske dokumenter • Privatlivspolitik
  • Sikkerhedspolitik
الْعَرَبيّة 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 er en samling open source-forretningsapps, der dækker alle dine virksomhedsbehov – lige fra CRM, e-handel og bogføring til lagerstyring, POS, projektledelse og meget mere.

Det unikke ved Odoo er, at systemet både er brugervenligt og fuldt integreret.

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