microapi.serializers

class microapi.serializers.ModelSerializer

A stupid-simple object to handle serializing/deserializing Model data.

If you have even the slightest of complex needs, you’re better off handling serialization manually.

collect_field_names(model)

Given a Model instance, collects all the field names from it.

This includes any parent fields. It excludes: * Related fields * Generated fields * Virtual fields

Parameters:

model (Model) – A Django Model instance (not the class).

Returns:

A sorted list of all field names.

Return type:

list

from_dict(model, data, strict=False)

Loads data from a dictionary onto a Model instance.

If there are keys in the data that you do NOT want populated on the Model (such as primary keys, “private” fields, etc.), you should pop those values before passing to this method.

Parameters:
  • model (Model) – A Model instance.

  • data (dict) – The data provided by a user.

  • strict (bool) – (Optional) If True, requires all keys in the data to match to an existing Model field. Default is False.

Returns:

The populated (but unsaved) model instance.

Return type:

Model

to_dict(model, exclude=None)

Converts a populated Model’s fields/values into a plain dictionary.

Parameters:
  • model (Model) – A populated Model instance.

  • exclude (list) – A list of fields to exclude from the final dict. Default is [].

Returns:

A dictionary of the field names/values.

Return type:

OrderedDict