Přejít na obsah
Odoo Menu
  • Přihlásit se
  • Vyzkoušejte zdarma
  • Aplikace
    Finance
    • Účetnictví
    • Fakturace
    • Výdaje
    • Spreadsheet (BI)
    • Dokumenty
    • Podpisy
    Prodej
    • CRM
    • Prodej
    • POS Obchod
    • POS Restaurace
    • Předplatné
    • Pronájem
    Webové stránky
    • Webové stránky
    • E-shop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Dodavatelský řetězec
    • Sklad
    • Výroba
    • PLM
    • Nákup
    • Údržba
    • Kvalita
    Lidské zdroje
    • Zaměstnanci
    • Nábor
    • Volno
    • Hodnocení zaměstnanců
    • Doporučení
    • Vozový park
    Marketing
    • Marketing sociálních sítí
    • Emailový marketing
    • SMS Marketing
    • Události
    • Marketingová automatizace
    • Dotazníky
    Služby
    • Projekt
    • Časové výkazy
    • Práce v terénu
    • Helpdesk
    • Plánování
    • Schůzky
    Produktivita
    • Diskuze
    • Schvalování
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Aplikace třetích stran Odoo Studio Odoo cloudová platforma
  • Branže
    Maloobchod
    • Knihkupectví
    • Obchod s oblečením
    • Obchod s nábytkem
    • Potraviny
    • Obchod s hardwarem
    • Hračkářství
    Jídlo a pohostinství
    • Bar a Pub
    • Restaurace
    • Fast Food
    • Penzion
    • Distributor nápojů
    • Hotel
    Nemovitost
    • Realitní kancelář
    • Architektonická firma
    • Stavba
    • Správa nemovitostí
    • Zahradnictví
    • Asociace vlastníků nemovitosti
    Poradenství
    • Účetní firma
    • Odoo Partner
    • Marketingová agentura
    • Právník
    • Akvizice talentů
    • Audit a certifikace
    Výroba
    • Textil
    • Kov
    • Nábytek
    • Jídlo
    • Pivovar
    • Korporátní dárky
    Zdraví a fitness
    • Sportovní klub
    • Prodejna brýli
    • Fitness Centrum
    • Wellness praktikové
    • Lékárna
    • Kadeřnictví
    Transakce
    • Údržbář
    • Podpora IT & hardware
    • Systémy solární energie
    • Výrobce obuvi
    • Úklidové služby
    • Služby HVAC
    Ostatní
    • Nezisková organizace
    • Agentura pro životní prostředí
    • Pronájem billboardů
    • Fotografování
    • Leasing jízdních kol
    • Prodejce softwaru
    Procházet všechna odvětví
  • Komunita
    Edukační program
    • Tutoriály
    • Dokumentace
    • Certifikace
    • Vzdělávání
    • Blog
    • Podcast
    Podpora vzdělávání
    • Vzdělávací program
    • Scale Up! Hra na firmu
    • Navštivte Odoo
    Získat software
    • Stáhnout
    • Porovnejte edice
    • Verze
    Spolupráce
    • Github
    • Fórum
    • Události
    • Překlady
    • Stát se partnerem
    • Služby pro partnery
    • Registrujte svou účetní firmu
    Získat služby
    • Najít partnera
    • Najít účetní
    • Setkejte se s poradcem
    • Implementační služby
    • Zákaznické reference
    • Podpora
    • Upgrady
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dohodnout demo
  • Ceník
  • Pomoc

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

  • CRM
  • e-Commerce
  • Účetnictví
  • Sklad
  • PoS
  • Projekty
  • MRP
All apps
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
Pomoc

Is it possible to enforce a float field in a form to accept values only between 0 and 99?

Odebírat

Get notified when there's activity on this post

This question has been flagged
viewsfieldsxmlformmargin
5 Odpovědi
8514 Zobrazení
Avatar
E.M.

I am looking the user to enter a % (sales margin) between 0 and 99.

Is it possible to enforce that the float field will accept only values between x and y?

If so, how?,

and,

would it be possible to popup a message informing the user that he has to enter something between that x and y values?


Note: I am not looking to know how to add the field, just how to enforce that a defined field accept values only between x and y.

0
Avatar
Zrušit
Avatar
E.M.
Autor Nejlepší odpověď

I added the constraint as stated in the 'Model constraints' section of documentation but it does not seem to trigger anything, does it have to be included within the class definition? Is there something missing?

# -*- coding: utf-8 -*-
from openerp import models, fields, api
from openerp.exceptions import ValidationError

class sale_order_line(models.Model):

_inherit = "sale.order.line"

sale_margin_percent = fields.Float('Margen de Ventas (%)', (4, 2))

def update_sale_margin(self, cr, uid, ids, price_unit, purchase_price, discount):
print 'update_sale_margin'
....
return {'value': {'sale_margin_percent': sale_margin } }

def update_sale_price(self, cr, uid, ids, sale_margin_percent, purchase_price, discount):
print 'update_sale_price'
....
        return {'value': {'price_unit': sale_price } }

@api.one
@api.constrains('sale_margin_percent')
def _check_margin(self):
print '_check_margin'
if self.sale_margin_percent >= 100:
raise ValidationError("El margen de venta no puede ser superior al 100%")
# all records passed the test, don't return anything


[****UPDATE*****]

Following zbik comment, I have rewritten the code as follows:


# -*- coding: utf-8 -*-
from openerp import models, fields, api, exceptions

class sale_order_line(models.Model):

_inherit = "sale.order.line"

sale_margin_percent = fields.Float('Margen de Ventas (%)', (4, 2))

@api.onchange('price_unit')
def update_sale_margin(self, price_unit, purchase_price, discount):
print 'update_sale_margin'
...
        return {'value': {'sale_margin_percent': sale_margin } }

    @api.onchange('sale_margin_percent', 'purchase_price')
    def update_sale_price(self, sale_margin_percent, purchase_price, discount):
       print 'update_sale_price'
...
        return {'value': {'price_unit': sale_price } }

    @api.constrains('sale_margin_percent')
    def _check_margin(self):
        print '_check_margin'
        if self.sale_margin_percent >= 100:
            raise exceptions.ValidationError("El margen de venta no puede ser superior al 100%")
         # all records passed the test, don't return anything


The attribute added is:

<xpath expr="//field[@name='order_line']/tree//field[@name='price_unit']" position="attributes">
<attribute name="on_change">update_sale_margin(price_unit, purchase_price, discount)</attribute>
</xpath>


But I get the following error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 537, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 574, in dispatch
    result = self._call_function(**self.params)
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 310, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 113, in wrapper
    return f(dbname, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 307, in checked_call
    return self.endpoint(*a, **kw)
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 803, in __call__
    return self.method(*args, **kw)
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 403, in response_wrap
    response = f(*args, **kw)
  File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 944, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 936, in _call_kw
    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
    return old_api(self, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 363, in old_api
    result = method(recs, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 5864, in onchange
    record._onchange_eval(name, field_onchange[name], result)
  File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 5782, in _onchange_eval
    method_res = getattr(self._model, method)(*args)
TypeError: update_sale_margin() takes exactly 4 arguments (7 given) 


Hope someone can help.


0
Avatar
Zrušit
Zbik

You mix old and new api. on_change in new api works with decorator @api.onchange. Probably in this trouble.

E.M.
Autor

Thanks zbik, I have tried to unify to new api, but I can't make the on_change update functions work with the new api. You can see code above.

Avatar
Zbik
Nejlepší odpověď

See Model constraints in documentation. Example from doc:

from openerp.exceptions import ValidationError

@api.constrains('age')
def _check_something(self):
    for record in self:
        if record.age > 20:
            raise ValidationError("Your record is too old: %s" % record.age)
    # all records passed the test, don't return anything 


0
Avatar
Zrušit
E.M.
Autor

I added the constraint as stated in the 'Model constraints' section of documentation but it does not seem to trigger anything, does it have to be included within the class definition? Is there something missing? # -*- coding: utf-8 -*- from openerp import models, fields, api class sale_order_line(models.Model): _inherit = "sale.order.line" sale_margin_percent = fields.Float('Margen de Ventas (%)', (4, 2)) def update_sale_margin(self, cr, uid, ids, price_unit, purchase_price, discount): print 'update_sale_margin' .... return {'value': {'sale_margin_percent': sale_margin } } def update_sale_price(self, cr, uid, ids, sale_margin_percent, purchase_price, discount): print 'update_sale_price' ... return {'value': {'price_unit': sale_price } } @api.one @api.constrains('sale_margin_percent') def _check_margin(self): print '_check_margin' if self.sale_margin_percent >= 100: raise ValidationError("El margen de venta no puede ser superior al 100%") # all records passed the test, don't return anything

E.M.
Autor

Sorry, see attached answer with proper formatting (-not enough karma to convert as an answer or delete de comment-).

Avatar
Tarek Mohamed Ibrahim
Nejlepší odpověď

I think you have to write an 'on_change' method and attache it in the xml to the field you need to enforce its value

field in xml will be like the following:

...
<field name="percent_field" on_change="onchange_percent_field(percent_field)"/>
...

you have to create a method onchange_percent_field and put in it your check, if the value is out of your range then you have to set the value of the field to its old value and  raise an exception 


0
Avatar
Zrušit
E.M.
Autor

I would prefer to let the on_change function for actual on_change activities. I see that more as a trigger than a constraint.

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

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

Přihlásit se
Related Posts Odpovědi Zobrazení Aktivita
Hello, I would like to know where I can edit the xml of this part of the form view
views xml form
Avatar
Avatar
1
lis 22
4197
How to align checkboxes, in a single line in form? Vyřešeno
views fields form V17
Avatar
Avatar
Avatar
2
čvc 24
2732
Inherited form view for specific module without overriding base view - Odoo 15.0
views modules xml form
Avatar
Avatar
1
bře 22
4085
Odoo 13: Form view inherit Vyřešeno
views xml form html
Avatar
Avatar
Avatar
Avatar
4
čvn 21
7788
How to have multiple views inside a xml file? Vyřešeno
views fields xml resources
Avatar
Avatar
1
led 21
5057
Komunita
  • Tutoriály
  • Dokumentace
  • Fórum
Open Source
  • Stáhnout
  • Github
  • Runbot
  • Překlady
Služby
  • Odoo.sh hostování
  • Podpora
  • Upgrade
  • Nestandardní vývoj
  • Edukační program
  • Najít účetní
  • Najít partnera
  • Stát se partnerem
O nás
  • Naše společnost
  • Podklady značky
  • Kontakujte nás
  • Práce
  • Události
  • Podcast
  • Blog
  • Zákazníci
  • Právní dokumenty • Soukromí
  • Zabezpečení
الْعَرَبيّة 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 balíček open-source aplikací, které pokrývají všechny potřeby vaší společnosti: CRM, e-shop, účetnictví, sklady, kasy, projektové řízení a další.

Unikátní nabídka od Odoo poskytuje velmi jednoduché uživatelské rozhraní a vše je integrované na jednom místě.

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