'Handles the saving of related M2M data.
Due to the way Django works, the M2M data must be handled after the
main instance, which is why this isn\'t a part of the main ``save`` bits.
Currently slightly inefficient in that it will clear out the whole
relation and recreate the related data as needed.'

def save_m2m(self, bundle):

    for (field_name, field_object) in self.fields.items():
        if (not field_object.is_m2m):
            continue
        if (not field_object.attribute):
            continue
        if field_object.readonly:
            continue
        related_mngr = None
        if isinstance(field_object.attribute, six.string_types):
            related_mngr = getattr(bundle.obj, field_object.attribute)
        elif callable(field_object.attribute):
            related_mngr = field_object.attribute(bundle)
        if (not related_mngr):
            continue
        if hasattr(related_mngr, u'clear'):
            related_mngr.clear()
        related_objs = []
        for related_bundle in bundle.data[field_name]:
            related_resource = field_object.get_related_resource(bundle.obj)
            updated_related_bundle = related_resource.build_bundle(obj=related_bundle.obj, data=related_bundle.data, request=bundle.request, objects_saved=bundle.objects_saved, via_uri=related_bundle.via_uri)
            related_resource.save(updated_related_bundle)
            related_objs.append(updated_related_bundle.obj)
        related_mngr.add(*related_objs)
