Enumeration and property functions

52 views
Skip to first unread message

Sebastian Stetter

unread,
Jun 28, 2016, 4:39:05 AM6/28/16
to Project Camelot
Hi,

I have an Enumeration field in my Entity 'Payment'
amount_is = Column(camelot.types.Enumeration([(0,u'inkl. Steuer'), (1,u"excl. Steuer"),  (2,u"nur Steuer")]),  index = True, nullable=False,  default=u"inkl. Steuer")


In order to perform a calculation, each time this field is changed by the user, I have written a property funtion as a hook:

    #_amount_is hook
   
def _set_amount_is(self,  amount_is):
       
self.amount_is = amount_is
       
self.flush()
       
self.update_amounts()

   
def _get_amount_is(self):
       
return self.amount_is

    _amount_is
= property(_get_amount_is, _set_amount_is)

I adjusted Admin accordingly and set the ComboBoxDelegate as delegate:
    class Admin(EntityAdmin):
        verbose_name
= _('Payment')
        validator
= PaymentValidator

#form
        form_display
= ['_entered_amount', '_amount_is','_tax','amount_excl', 'amount_incl', 'amount_tax']
        field_attributes
= dict(
            _amount_is
=dict(delegate=delegates.ComboBoxDelegate, target="Payment",  editable=True),
            _entered_amount
=dict(delegate=delegates.FloatDelegate,  editable=True),
            _tax
=dict(delegate=delegates.Many2OneDelegate, target='Tax',  editable=True)
       
)



Now the problem is, that the Choices delegate is not supplied with my list of choices, so I get some Errors like this:
ERROR:camelot.view.controls.editors.ChoicesEditor:Could not set value nur Steuer in field _amount_is_editor because it is not in the list of choices

What is the proper way to supply the ComboBoxDelegate with the list of choices in this case?

Thanks for your help, guys
Sebastian

Sebastian Stetter

unread,
Jun 28, 2016, 4:50:47 AM6/28/16
to Project Camelot
Oh and while we are at it... what would be a good approach to include the choices in the translation?

Erik Janssens

unread,
Jun 28, 2016, 12:38:40 PM6/28/16
to project...@googlegroups.com
Hello Sebastian,

you should use the `choices` field attribute for this,
as in :

choices = [(u'inkl. Steuer', _('inkl. Steuer')), ...

Cheers,

Erik
--
--
You received this message because you are subscribed to the "Project Camelot" group.
Visit www.python-camelot.com for more information
 
To post to this group, send email to project...@googlegroups.com
To unsubscribe from this group, send email to
project-camel...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/project-camelot?hl=en

---
You received this message because you are subscribed to the Google Groups "Project Camelot" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-camel...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Erik Janssens

unread,
Jun 28, 2016, 12:41:44 PM6/28/16
to project...@googlegroups.com
The first element in a choice tuple should be the value of the property,
while the second element is the text shown to the user.

So, the second element can be of type ugettext_lazy

from camelot.core.utils import ugettext_lazy as _

choices = ((1, _('One'), (2, _('Two'), ...

Sebastian Stetter

unread,
Jun 28, 2016, 12:58:16 PM6/28/16
to Project Camelot, erik.j...@conceptive.be
Thanks, Erik!
You are awesome. Should I have found that somewhere in the documentation?

Best, Sebastian
Message has been deleted

Sebastian Stetter

unread,
Jun 28, 2016, 1:25:15 PM6/28/16
to Project Camelot, erik.j...@conceptive.be
Now it kind of works. The only problem i have left wit this, is that I somehow can't set a default choice anymore. I tried bot, specifying it in the Entity as usual and adding it to the admin. This line now looks like this:

_amount_is=dict(delegate=delegates.ComboBoxDelegate, target="Payment",  editable=True,  choices = ((0, _(u"incl. tax")), (1, _(u"excl. tax")),  (2, _(u"tax only")))), default=_(u"incl. tax"),

Also, am I right in the assumtion that, If I want choices to be translated, i always have to specify that in the field_properties?

Thanks, man!

Reply all
Reply to author
Forward
0 new messages