Esempio n. 1
0
    def obj_create(self, bundle, **kwargs):
        try:
            bundle.obj = self._meta.object_class()

            for key, value in kwargs.items():
                setattr(bundle.obj, key, value)

            bundle = self.full_hydrate(bundle)

            object_list = getattr(self.instance, self.attribute)
            pk_field = getattr(self._meta, 'id_field', None)

            if pk_field is None:
                bundle.obj.pk = len(object_list)
            else:
                bundle.obj.__class__.pk = tastypie_mongoengine_fields.link_property(
                    pk_field)

            object_list.append(bundle.obj)

            self.save_related(bundle)

            self.instance.save()

            m2m_bundle = self.hydrate_m2m(bundle)
            self.save_m2m(m2m_bundle)
            return bundle
        except mongoengine.ValidationError as ex:
            raise exceptions.ValidationError(ex.message)
    def obj_create(self, bundle, **kwargs):
        try:
            bundle.obj = self._meta.object_class()

            for key, value in kwargs.items():
                setattr(bundle.obj, key, value)

            bundle = self.full_hydrate(bundle)

            object_list = getattr(self.instance, self.attribute)
            pk_field = getattr(self._meta, 'id_field', None)

            if pk_field is None:
                bundle.obj.pk = len(object_list)
            else:
                bundle.obj.__class__.pk = tastypie_mongoengine_fields.link_property(pk_field)

            object_list.append(bundle.obj)

            self.save_related(bundle)

            self.instance.save()

            m2m_bundle = self.hydrate_m2m(bundle)
            self.save_m2m(m2m_bundle)
            return bundle
        except mongoengine.ValidationError as ex:
            raise exceptions.ValidationError(ex.message)
    def get_object_list(self, request):
        if not self.instance:
            return ListQuerySet()

        pk_field = getattr(self._meta, 'id_field', None)

        if pk_field is not None:
            object_list = []
            for obj in getattr(self.instance, self.attribute):
                pk = getattr(obj, pk_field)
                obj.__class__.pk = tastypie_mongoengine_fields.link_property(pk_field)
                object_list.append((unicode(pk), obj))
            return ListQuerySet(object_list)

        else:
            def add_index(index, obj):
                obj.pk = index
                return obj

            return ListQuerySet([(unicode(index), add_index(index, obj)) for index, obj in enumerate(getattr(self.instance, self.attribute))])
Esempio n. 4
0
    def get_object_list(self, request):
        if not self.instance:
            return ListQuerySet()

        pk_field = getattr(self._meta, 'id_field', None)

        if pk_field is not None:
            object_list = []
            for obj in getattr(self.instance, self.attribute):
                pk = getattr(obj, pk_field)
                obj.__class__.pk = tastypie_mongoengine_fields.link_property(pk_field)
                object_list.append((str(pk), obj))
            return ListQuerySet(object_list)

        else:
            def add_index(index, obj):
                obj.pk = index
                return obj

            return ListQuerySet([(str(index), add_index(index, obj)) for index, obj in enumerate(getattr(self.instance, self.attribute))])