示例#1
0
    def dehydrate(self, bundle, for_list=True):
        the_m2ms = None

        if isinstance(self.attribute, basestring):
            the_m2ms = getattr(bundle.obj, self.attribute)
        elif callable(self.attribute):
            the_m2ms = self.attribute(bundle)

        if not the_m2ms:
            if not self.null:
                raise ApiFieldError(
                    "The document %r has an empty attribute '%s' and does not allow a null value."
                    % (bundle.obj, self.attribute))
            return {}

        self.m2m_resources = {}
        m2m_dehydrated = {}

        # the_m2ms is a list, not a queryset
        for m2m_key, m2m_obj in the_m2ms.iteritems():
            m2m_resource = self.get_related_resource(m2m_obj)
            m2m_bundle = tastypie_bundle.Bundle(obj=m2m_obj,
                                                request=bundle.request)
            self.m2m_resources[m2m_key] = m2m_resource
            m2m_dehydrated[m2m_key] = self.dehydrate_related(m2m_bundle,
                                                             m2m_resource,
                                                             for_list=for_list)
        return m2m_dehydrated
    def dehydrate(self, bundle):
        assert bundle.obj

        the_m2ms = None

        if isinstance(self.attribute, basestring):
            the_m2ms = getattr(bundle.obj, self.attribute)
        elif callable(self.attribute):
            the_m2ms = self.attribute(bundle)

        if not the_m2ms:
            if not self.null:
                raise exceptions.ApiFieldError(
                    "The document %r has an empty attribute '%s' and does not allow a null value."
                    % (bundle.obj, self.attribute))
            return []

        self.m2m_resources = []
        m2m_dehydrated = []

        for index, m2m in enumerate(the_m2ms):
            m2m.pk = index
            m2m.parent = bundle.obj
            m2m_resource = self.get_related_resource(m2m)
            m2m_bundle = tastypie_bundle.Bundle(obj=m2m,
                                                request=bundle.request)
            self.m2m_resources.append(m2m_resource)
            m2m_dehydrated.append(
                self.dehydrate_related(m2m_bundle, m2m_resource))

        return m2m_dehydrated
示例#3
0
文件: fields.py 项目: tcpr1/vosae-app
    def dehydrate(self, bundle, for_list=True):
        the_m2ms = None

        if isinstance(self.attribute, basestring):
            the_m2ms = getattr(bundle.obj, self.attribute)
        elif callable(self.attribute):
            the_m2ms = self.attribute(bundle)

        if not the_m2ms:
            if not self.null:
                raise exceptions.ApiFieldError(
                    "The document %r has an empty attribute '%s' and does not allow a null value."
                    % (bundle.obj, self.attribute))
            return []

        self.m2m_resources = []
        m2m_dehydrated = []

        # the_m2ms is a list, not a queryset
        for m2m in the_m2ms:
            m2m_resource = self.get_related_resource(m2m)
            m2m_bundle = tastypie_bundle.Bundle(obj=m2m,
                                                request=bundle.request)
            self.m2m_resources.append(m2m_resource)
            m2m_dehydrated.append(
                self.dehydrate_related(m2m_bundle,
                                       m2m_resource,
                                       for_list=for_list))

        return m2m_dehydrated
    def dehydrate(self, bundle, for_list=True):
        assert bundle.obj

        the_m2ms = None

        if isinstance(self.attribute, basestring):
            the_m2ms = getattr(bundle.obj, self.attribute)
        elif callable(self.attribute):
            the_m2ms = self.attribute(bundle)

        if not the_m2ms:
            if not self.null:
                raise exceptions.ApiFieldError(
                    "The document %r has an empty attribute '%s' and does not allow a null value."
                    % (bundle.obj, self.attribute))
            return []

        self.m2m_resources = []
        m2m_dehydrated = []

        # the_m2ms is a list, not a queryset
        for index, m2m in enumerate(the_m2ms):
            m2m.parent = bundle.obj
            m2m_resource = self.get_related_resource(m2m)

            pk_field = getattr(m2m_resource._meta, 'id_field', None)
            if pk_field is None:
                m2m.pk = index
            else:
                m2m.__class__.pk = link_property(pk_field)

            m2m_bundle = tastypie_bundle.Bundle(obj=m2m,
                                                request=bundle.request)
            self.m2m_resources.append(m2m_resource)
            if tastypie.__version__ >= (0, 9, 15):
                m2m_dehydrated.append(
                    self.dehydrate_related(m2m_bundle,
                                           m2m_resource,
                                           for_list=for_list))
            else:
                m2m_dehydrated.append(
                    self.dehydrate_related(m2m_bundle, m2m_resource))

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

            return []

        the_m2ms = None

        if isinstance(self.attribute, basestring):
            the_m2ms = getattr(bundle.obj, self.attribute)
        elif callable(self.attribute):
            the_m2ms = self.attribute(bundle)

        if not the_m2ms:
            if not self.null:
                raise exceptions.ApiFieldError(
                    "The document %r has an empty attribute '%s' and does not allow a null value."
                    % (bundle.obj, self.attribute))
            return []

        self.m2m_resources = []
        m2m_dehydrated = []

        # the_m2ms is a list, not a queryset
        for m2m in the_m2ms:
            m2m_resource = self.get_related_resource(m2m)
            m2m_bundle = tastypie_bundle.Bundle(obj=m2m,
                                                request=bundle.request)
            self.m2m_resources.append(m2m_resource)
            if tastypie.__version__ >= (0, 9, 15):
                m2m_dehydrated.append(
                    self.dehydrate_related(m2m_bundle,
                                           m2m_resource,
                                           for_list=for_list))
            else:
                m2m_dehydrated.append(
                    self.dehydrate_related(m2m_bundle, m2m_resource))

        return m2m_dehydrated