Fields provide the data coersion and validation when pulling data from FileMaker. All fields should inherit from the :pyBaseFileMakerField
.
The following fields are provided by Django filemaker.
filemaker.fields
If your custom field only requires a simple validation check, then it is easiest to override the validators list for a field by passing in a new list of validators.
If you require more control over your field, you can subclass :pyBaseFileMakerField
, or the field class that most closely resembles your desired type. The two methods that you will likely wish to overwrite are the :pyBaseFileMakerField.coerce
method, and the :pyBaseFileMakerField.to_django
method.
:pyBaseFileMakerField.coerce
is called by the private _coerce
method during validation. It should take a single value
parameter and either return an instance of the required type, or raise a :pyfilemake.exceptions.FileMakerValidationError
with an explanation of why the value could not be coerced.
:pyBaseFileMakerField.to_django
does any post processing on the field required to render it suitable for passing into a Django field. By default this method just returns the field instances' current value.
As an example, if we wanted to have a field that took a string and added "from FileMaker" to the end of it's value we could do: :
from filemaker.fields import CharField
class FromFileMakerCharField(CharField):
def coerce(self, value):
text = super(FromFileMakerCharField, self).coerce(value)
return '{0} from FileMaker'.format(text)
If we wanted to remove the extra string before passing the value into a Django model, we could add a :pyBaseFileMakerField.to_django
method, like so: :
import re
from filemaker.fields import CharField
class FromFileMakerCharField(CharField):
def coerce(self, value):
text = super(FromFileMakerCharField, self).coerce(value)
return '{0} from FileMaker'.format(text)
def to_django(self):
return re.sub(r' from FileMaker$', '', self.value)