Beispiel #1
0
    def serialize(self, instance):
        if not isinstance(instance, self.model_class):
            raise TypeError(str(self.__class__),
                            'Instance must match model class')

        obj_dict = {
            '_meta': {
                'id': instance.id,
                'model': instance._meta.object_name,
                'app': instance._meta.app_label,
            },
            '_id': doc_id_for_instance(instance),
        }

        for f in self._simple_field_names:
            try:
                field = self.model_class._meta.get_field(f)
                obj_dict[f] = field.value_from_object(instance)
            except FieldDoesNotExist:
                continue

        for f in self._rel_field_names:
            rel_map = getattr(self, f)
            rel_value = getattr(instance, f, None)
            if rel_value:
                obj_dict[f] = rel_map.serialize(
                    rel_value.all() if rel_map.many else rel_value)

        for f in self._choice_field_names:
            get_display_value = getattr(instance, 'get_{}_display'.format(f),
                                        None)
            if get_display_value:
                obj_dict[f] = get_display_value()

        for field_name, field_func_name in self._attribute_field_map.items():
            func = getattr(self, field_func_name)
            obj_dict[field_name] = func(instance)

        return obj_dict
Beispiel #2
0
    def serialize(self, instance):
        if not isinstance(instance, self.model_class):
            raise TypeError(str(self.__class__), 'Instance must match model class')

        obj_dict = {
            '_meta': {
                'id': instance.id,
                'model': instance._meta.object_name,
                'app': instance._meta.app_label,
            },
            '_id': doc_id_for_instance(instance),
        }

        for f in self._simple_field_names:
            try:
                field = self.model_class._meta.get_field(f)
                obj_dict[f] = field.value_from_object(instance)
            except FieldDoesNotExist:
                continue

        for f in self._rel_field_names:
            rel_map = getattr(self, f)
            rel_value = getattr(instance, f, None)
            if rel_value:
                obj_dict[f] = rel_map.serialize(rel_value.all() if rel_map.many else rel_value)

        for f in self._choice_field_names:
            get_display_value = getattr(instance, 'get_{}_display'.format(f), None)
            if get_display_value:
                obj_dict[f] = get_display_value()

        for field_name, field_func_name in self._attribute_field_map.items():
            func = getattr(self, field_func_name)
            obj_dict[field_name] = func(instance)

        return obj_dict
Beispiel #3
0
    def test_doc_id_for_instance(self):
        instance = EntryFactory(id=411)
        es_id = doc_id_for_instance(instance)

        self.assertIn('411', es_id)
        self.assertIn('writings.Entry', es_id)
Beispiel #4
0
    def test_doc_id_for_instance(self):
        instance = EntryFactory(id=411)
        es_id = doc_id_for_instance(instance)

        self.assertIn('411', es_id)
        self.assertIn('writings.Entry', es_id)