def dehydrate(self, bundle, for_list=True):
        if not bundle.obj or not bundle.obj.pk:
            if not self.null:
                raise ApiFieldError("The model '%r' does not have a primary key and can not be used in a ToMany context." % bundle.obj)

            return []

        the_m2ms = None
        previous_obj = bundle.obj
        attr = self.attribute

        if callable(self.attribute):
            the_m2ms = self.attribute(bundle)
        elif isinstance(self.attribute, six.string_types):
            the_m2ms = bundle.obj

            for attr in self._attrs:
                previous_obj = the_m2ms
                try:
                    the_m2ms = getattr(the_m2ms, attr, None)
                except __HOLE__:
                    the_m2ms = None

                if the_m2ms is None:
                    break

        if the_m2ms is None:
            if not self.null:
                raise ApiFieldError("The model '%r' has an empty attribute '%s' and doesn't allow a null value." % (previous_obj, attr))

        if isinstance(the_m2ms, models.Manager):
            the_m2ms = the_m2ms.all()

        m2m_dehydrated = [
            self.dehydrate_related(
                Bundle(obj=m2m, request=bundle.request),
                self.get_related_resource(m2m),
                for_list=for_list
            )
            for m2m in the_m2ms
        ]

        return m2m_dehydrated
dataset/ETHPy150Open django-tastypie/django-tastypie/tastypie/fields.py/ToManyField.dehydrate