Siirry sisältöön
Odoo Menu
  • Kirjaudu sisään
  • Kokeile ilmaiseksi
  • Sovellukset
    Talous
    • Kirjanpito
    • Laskutus
    • Kulut
    • Datataulukot (BI)
    • Asiakirjat
    • Allekirjoita
    Myynti
    • CRM
    • Myynti
    • Kassajärjestelmä myymälään
    • Kassajärjestelmä ravintolaan
    • Tilaukset
    • Vuokraus
    Verkkosivut
    • Verkkosivun Rakennustyökalu
    • Verkkokauppa
    • Blogi
    • Foorumi
    • Livechat
    • Verkko-oppiminen
    Toimitusketju
    • Varastointi
    • Tuotanto
    • Tuotteen elinkaaren hallinta (PLM)
    • Ostot
    • Huolto
    • Laatu
    Henkilöstöhallinto
    • Työntekijät
    • Rekrytointi
    • Vapaat
    • Arvioinnit
    • Suositukset
    • Kuljetuskalusto
    Markkinointi
    • Somemarkkinointi
    • Sähköpostimarkkinointi
    • Tekstiviestimarkkinointi
    • Tapahtumat
    • Markkinoinnin automaatio
    • Kyselyt
    Palvelut
    • Projekti
    • Työaikakirjaukset
    • Kenttähuolto
    • Asiakaspalvelu
    • Suunnittelu
    • Ajanvaraukset
    Tuottavuus
    • Viestintä
    • Hyväksynnät
    • IoT
    • IP-puhe
    • Tietokirjasto
    • WhatsApp
    Kolmannen osapuolen sovellukset Odoo-Studio Odoo-Pilvialusta
  • Toimialat
    Vähittäiskauppa
    • Kirjakauppa
    • Vaatekauppa
    • Huonekaluliike
    • Ruokakauppa
    • Laitteistokauppa
    • Lelukauppa
    Ruoka & Majoitus
    • Baari ja Pubi
    • Ravintola
    • Pikaruoka
    • Majatalo
    • Juomien jakelija
    • Hotelli
    Kiinteistöt
    • Kiinteistönvälitystoimisto
    • Arkkitehtitoimisto
    • Rakentaminen
    • Kiinteistönhallinta
    • Puutarhanhoito
    • Kiinteistön omistajien yhdistys
    Konsultointi
    • Tilitoimisto
    • Odoo-kumppani
    • Markkinointitoimisto
    • Lakitoimisto
    • Osaajahankinta
    • Tilintarkastus & sertifiointi
    Tuotanto
    • Tekstiili
    • Metalli
    • Huonekalut
    • Ruoka
    • Panimo
    • Yrityslahjat
    Terveys & Liikunta
    • Urheiluseura
    • Silmälasiliike
    • Kuntokeskus
    • Hyvinvointialan ammattilaiset
    • Apteekki
    • Kampaamo
    Kaupat
    • Yleismies
    • IT-laitteisto & Tuki
    • Aurinkoenergiajärjestelmät
    • Suutari
    • Siivouspalvelut
    • LVI-palvelut
    Muut
    • Voittoa tavoittelematon järjestö
    • Ympäristötoimisto
    • Mainostaulujen vuokraus  
    • Valokuvaus
    • Leasing-pyörät
    • Ohjelmistojen jälleenmyyjä
    Selaa kaikkia toimialoja
  • Yhteisö
    Opi
    • Kurssit
    • Dokumentaatio
    • Todistukset
    • Koulutus
    • Blogi
    • Podcast
    Kannusta kouluttautumaan
    • Koulutusohjelmat
    • Scale Up! Liiketoimintapeli
    • Vieraile Odoolla
    Hanki ohjelmisto
    • Lataa
    • Vertaile versioita
    • Julkaisut
    Tee yhteistyötä
    • Github
    • Foorumi
    • Tapahtumat
    • Käännökset
    • Ryhdy kumppaniksi
    • Kumppanipalvelut
    • Rekisteröi tilitoimistosi
    Hanki palveluja
    • Löydä kumppani
    • Löydä kirjanpitäjä
    • Varaa asiantuntijatapaaminen
    • Implementaatiopalvelut
    • Asiakasreferenssit
    • Tuki
    • Versionkorotukset
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Varaa demo
  • Hinnoittelu
  • Asiakaspalvelu

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

  • CRM
  • e-Commerce
  • Kirjanpito
  • Varastointi
  • PoS
  • Projekti
  • MRP
All apps
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Kaikki kirjoitukset Ihmiset Merkit
Tunnisteet (Näytä kaikki)
odoo accounting v14 pos v15
Tietoa tästä foorumista
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Kaikki kirjoitukset Ihmiset Merkit
Tunnisteet (Näytä kaikki)
odoo accounting v14 pos v15
Tietoa tästä foorumista
Apua

Best way to show json data on odoo UI

Tilaa

Saat ilmoituksen, kun tähän viestiin ilmaantuu aktiviteettia

Tämä kysymys on merkitty
jsonodoo11
2 Vastaukset
31125 Näkymät
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
Hylkää
Avatar
Yenthe Van Ginneken (Mainframe Monkey)
Paras vastaus

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
Hylkää
pawan
Tekijä

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
Paras vastaus

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
Hylkää
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 :)

Nautitko keskustelusta? Älä vain lue, vaan osallistu!

Luo tili jo tänään nauttiaksesi yksinoikeusominaisuuksista ja osallistuaksesi mahtavaan yhteisöömme!

Rekisteröidy
Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
How to return a simple json instead object JsonRpc? (Odoo11)
json jsonrpc odoo11
Avatar
Avatar
1
syysk. 20
7233
create backorder odoo after validate purchase shipment ? Ratkaistu
odoo11
Avatar
Avatar
1
lokak. 25
11862
I can't create any sub-task
odoo11
Avatar
Avatar
Avatar
2
heinäk. 24
3540
odoo 11 display calendar widget in website layout
odoo11
Avatar
Avatar
1
kesäk. 24
5874
Get field's value from another model
odoo11
Avatar
Avatar
1
lokak. 23
98
Yhteisö
  • Kurssit
  • Dokumentaatio
  • Foorumi
Avoin lähdekoodi
  • Lataa
  • Github
  • Runbot
  • Käännökset
Palvelut
  • Odoo.sh hosting
  • Tuki
  • Versionkorotus
  • Räätälöidyt kehitykset
  • Koulutus
  • Löydä kirjanpitäjä
  • Löydä kumppani
  • Ryhdy kumppaniksi
Meistä
  • Yrityksemme
  • Tavaramerkki
  • Ota yhteyttä
  • Työpaikat
  • Tapahtumat
  • Podcast
  • Blogi
  • Asiakkaat
  • Oikeudellinen ilmoitus • Yksityisyys
  • Tietoturva
الْعَرَبيّة 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 on kokoelma avoimen lähdekoodin yrityssovelluksia, jotka kattavat kaikki yrityksesi tarpeet: asiakkuudenhallinta eli CRM, verkkokauppa, kirjanpito, varastointi, kassajärjestelmä, projektinhallinta, jne.

Odoon uniikki arvolupaus on olla samanaikaisesti erittäin helppokäyttöinen ja täysin integroitu.

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