Request preprocessors are run before validating the POST data against the model.
Set a preprocessor on the API for POST
requests that parses any colors
key, altering the data
dictionary in-place:
def preprocess_colors(data):
colors = data.pop('colors', None)
if colors is not None:
# set primary and secondary colors
data['primary_color'] = get_primary_color(color)
data['secondary_color'] = get_secondary_color(colors)
You do need to remove the colors
key from the dictionary to prevent Flask-Restless from complaining about the key being there.
A postprocessor could never have worked here; these are only called to alter the returned response after the new instance has been created already.
Alternatively, create a SQLAlchemy hybrid attribute on your model named colors
that translates colors into primary and secondary colors in a setter.