def dict_to_model(instance, data, mapping=None):
    """Assign the values in the data dictonary to an instance.

    ``data`` is the cleaned data of the form.

    ``mapping`` is an optional dictionary that maps field names to attribute names,
    for example {'field': 'instance.child.field'}.

    """
    mapping = mapping or {}
    for key, value in data.items():
        key = mapping.get(key, key)
        bits = key.split('.')
        obj = instance
        for bit in bits[:-1]:
            obj = getattr(obj, bit)
        setattr(obj, bits[-1], value)
