I have a function used for defaultFactory parameter of a schema.Float field (from zope.schema). The field is supposed to be read only, so I set the readonly parameter to True. Also, the field is part of an interface for a behavior.
In my class, I have:
from zope.interface import alsoProvides, Interface, provider from zope.schema.interfaces import IContextAwareDefaultFactory, IContextSourceBinder from plone.autoform.interfaces import IFormFieldProvider @provider(IContextAwareDefaultFactory) def getExchangeRate(context): """Request is made to an api that provides an exchange rate for a specific day""" ..... return exchangeRate @provider(IContextSourceBinder) class IMyBehavior(Interface): value = schema.Float(title=_(u"Value"), default=0.00, required=True, ) currency = schema.Choice(title_(u"Currency", source=currencies, required=True, ) exchange_rate = schema.Float(title=_(u"Exchange Rate"), required=True, readonly=True, defaultFactory=getExchangeRate, ) alsoProvides(IMyInterface, IFormFieldProvider)
I have a content type that implements this behavior and there is a property that makes use of this function:
class IFundingSource(Interface): .... class FundingSource(Container): implements(IFundingSource) @property def us_dollar_value(self): amount = self.value * self.exchange_rate return amount
In my template for viewing the object using the property us_dollar_value:
I guess I can understand it being called in an edit form? But when viewing a BrowserView, the defaultFactory function is called. Why is this? Am I better off using an event that sets the field's value upon creation?
My version of Plone is 4.3