Skip to Content
Odoo Menu
  • Prihlásiť sa
  • Vyskúšajte zadarmo
  • Aplikácie
    Financie
    • Účtovníctvo
    • Fakturácia
    • Výdavky
    • Tabuľka (BI)
    • Dokumenty
    • Podpis
    Predaj
    • CRM
    • Predaj
    • POS Shop
    • POS Restaurant
    • Manažment odberu
    • Požičovňa
    Webstránky
    • Tvorca webstránok
    • eShop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Supply Chain
    • Sklad
    • Výroba
    • Správa životného cyklu produktu
    • Nákup
    • Údržba
    • Manažment kvality
    Ľudské zdroje
    • Zamestnanci
    • Nábor zamestnancov
    • Voľné dni
    • Hodnotenia
    • Odporúčania
    • Vozový park
    Marketing
    • Marketing sociálnych sietí
    • Email marketing
    • SMS marketing
    • Eventy
    • Marketingová automatizácia
    • Prieskumy
    Služby
    • Projektové riadenie
    • Pracovné výkazy
    • Práca v teréne
    • Helpdesk
    • Plánovanie
    • Schôdzky
    Produktivita
    • Tímová komunikácia
    • Schvalovania
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Priemyselné odvetvia
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Reštaurácia
    • Fast Food
    • Guest House
    • Beverage distributor
    • Hotel
    Reality
    • Real Estate Agency
    • Architecture Firm
    • Konštrukcia
    • Estate Managament
    • Gardening
    • Property Owner Association
    Poradenstvo
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Výroba
    • Textile
    • Metal
    • Furnitures
    • Jedlo
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware and Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Iní
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Komunita
    Vzdelávanie
    • Tutoriály
    • Dokumentácia
    • Certifikácie
    • Školenie
    • Blog
    • Podcast
    Empower Education
    • Vzdelávací program
    • Scale Up! Business Game
    • Visit Odoo
    Softvér
    • Stiahnuť
    • Porovnanie Community a Enterprise vierzie
    • Releases
    Spolupráca
    • Github
    • Fórum
    • Eventy
    • Preklady
    • Staň sa partnerom
    • Services for Partners
    • Register your Accounting Firm
    Služby
    • Nájdite partnera
    • Nájdite účtovníka
    • Meet an advisor
    • Implementation Services
    • Zákaznícke referencie
    • Podpora
    • Upgrades
    ​Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Získajte demo
  • Cenník
  • Pomoc

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

  • CRM
  • e-Commerce
  • Účtovníctvo
  • Sklady
  • PoS
  • Projektové riadenie
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tagy (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tagy (View all)
odoo accounting v14 pos v15
About this forum
Pomoc

How to add or delete fields depending on a checkbox field value

Odoberať

Get notified when there's activity on this post

This question has been flagged
fieldsmodelscheckboxodoov10
2 Replies
7544 Zobrazenia
Avatar
jonathan perez

Hello I'm making a module ( I'm newbie on odoo),  i want add or delete fields depending if a checkbox field value is yes or no, i have this code on my model:

# -*- coding: utf-8 -*-

from odoo import models, fields, api
import time
import requests

class Modulo2(models.Model):
_name = 'modulo2.modulo2'
urlApi = 'http://localhost:8000/api/persons/'
actualYear = time.strftime('%Y')
newPerson = fields.Selection([('yes', 'Yes'), ('no', 'No')], string='Nueva Persona (Si/No)')

if newPerson == 'yes':
nombre = fields.Char(string="Nombre", required=True)
else:
nombre = fields.Char(string="Nombre", required=True)
anioNacimiento = fields.Integer(string="Año de Nacimiento", compute="_obtener_datos", store=True)
edad = fields.Integer(compute="_calcular_edad", store=True)

@api.depends('nombre')
def _obtener_datos(self):
response = requests.get(str(self.urlApi) + str(self.nombre))
if response.text != "not found":
self.anioNacimiento = response.json()['year']

@api.depends('anioNacimiento')
def _calcular_edad(self):
self.edad = int(self.actualYear) - int(self.anioNacimiento)


But this doesn't work, any idea? i use odoo 10 community version, ( sorry for my bad english ), thanks !




0
Avatar
Zrušiť
Avatar
jonathan perez
Autor Best Answer

Hello, I do not understand much about the xml this is my xml file,


<odoo>
<data>
<!-- explicit list view definition -->
<record model="ir.ui.view" id="modulo2.list">
<field name="name">modulo2 list</field>
<field name="model">modulo2.modulo2</field>
<field name="view_mode">tree,form</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
<field name="anioNacimiento"/>
<field name="edad"/>
</tree>
<form>
<sheet string="modulo2.modulo2">
<group col="4">
<field name="edad" modifiers="{'readonly': true}"/>
<field name="name" on_change="1" modifiers="{}"/>
<field name="anioNacimiento" on_change="1" modifiers="{'readonly': true}"/>
<field name="newPerson" modifiers="{}" invisible="1"/>
<separator/>
</group>
</sheet>
</form>
</field>
</record>


<!-- actions opening views on models -->

<record model="ir.actions.act_window" id="modulo2.action_window">
<field name="name">modulo2 prueba </field>
<field name="res_model">modulo2.modulo2</field>
<field name="view_mode">tree,form</field>
</record>


<!-- server action to the one above -->



<!-- Top menu item -->

<menuitem name="modulo2" id="modulo2.menu_root"/>

<!-- menu categories -->

<menuitem name="Menu" id="modulo2.menu_1" parent="modulo2.menu_root"/>

<!-- actions -->

<menuitem name="Personas" id="modulo2.menu_1_list" parent="modulo2.menu_1"
action="modulo2.action_window"/>
</data>
</odoo>


which part should I modify ?, I would also like to edit the form that appears when the button is given to create, but I have not managed to do it, I have tried many forms, but I do not finish achieve it, if you could help me it would be great, thank you very much

0
Avatar
Zrušiť
Kevin

<field name="newPerson"/>

<field name="nombre" attrs="{'invisible':[('newPerson', '=', 'no')], 'required':[('newPerson', '=', 'yes')]}" />

Avatar
Kevin
Best Answer

Just declare the field in the .py file and use attrs in the xml file

like

newPerson = fields.Selection([('yes', 'Yes'), ('no', 'No')], string='Nueva Persona (Si/No)')
nombre = fields.Char(string="Nombre")

instead of


newPerson = fields.Selection([('yes', 'Yes'), ('no', 'No')], string='Nueva Persona (Si/No)')

if newPerson == 'yes':
nombre = fields.Char(string="Nombre")
else:
nombre = fields.Char(string="Nombre", required=True)

and in your xml file, when you declare your filed nombre, use attrs like


<field name="nombre" attrs="{'invisible':[('newPerson', '=', 'no')], 'required':[('newPerson', '=', 'yes')]}" />

 

0
Avatar
Zrušiť
Enjoying the discussion? Don't just read, join in!

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

Registrácia
Related Posts Replies Zobrazenia Aktivita
View a field from one model in another model Solved
fields models
Avatar
Avatar
1
nov 25
6113
How to find the field type? Solved
fields models
Avatar
Avatar
1
jún 25
5921
Design a field value verification (not odoo.api.constrains validation)
fields models
Avatar
Avatar
Avatar
3
júl 20
12748
Add base field (default_code) to a model (stock.quant)
fields models
Avatar
0
nov 16
5270
Calculate fields from two different models
fields models
Avatar
Avatar
1
aug 15
5558
Komunita
  • Tutoriály
  • Dokumentácia
  • Fórum
Open Source
  • Stiahnuť
  • Github
  • Runbot
  • Preklady
Služby
  • Odoo.sh hosting
  • Podpora
  • Vyššia verzia
  • Custom Developments
  • Vzdelávanie
  • Nájdite účtovníka
  • Nájdite partnera
  • Staň sa partnerom
O nás
  • Naša spoločnosť
  • Majetok značky
  • Kontaktujte nás
  • Pracovné ponuky
  • Eventy
  • Podcast
  • Blog
  • Zákazníci
  • Právne dokumenty • Súkromie
  • Bezpečnosť
الْعَرَبيّة 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 je sada podnikových aplikácií s otvoreným zdrojovým kódom, ktoré pokrývajú všetky potreby vašej spoločnosti: CRM, e-shop, účtovníctvo, skladové hospodárstvo, miesto predaja, projektový manažment atď.

Odoo prináša vysokú pridanú hodnotu v jednoduchom použití a súčasne plne integrovanými biznis aplikáciami.

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