Exemplo n.º 1
0
class SnippetDocument(DocType):
    """
    Document for the Snippet model for elasticsearch with fields to index
    """
    desc = TextField(
        attr='desc',
        fields={
            'suggest': Completion(),
        }
    )
    user = ObjectField(
        properties={
            'username': TextField(),
            'id': IntegerField(),
        }
    )

    class Django:
        model = Snippet
        fields = [
            'id',
            'is_visible'
        ]

        related_models = [settings.AUTH_USER_MODEL]

    def get_queryset(self):
        return super().get_queryset().select_related(
            'user'
        )

    def get_instances_from_related(self, related_instance):
        if isinstance(related_instance, settings.AUTH_USER_MODEL):
            return related_instance.snippet_set.all()
    def test_get_value_from_instance_with_none_inner_objectfield(self):
        field = ObjectField(attr='person', properties={
            'first_name': TextField(analyzer='foo'),
            'last_name': TextField(),
            'additional': ObjectField(properties={
                'age': IntegerField()
            })
        })

        instance = NonCallableMock(person=NonCallableMock(
            first_name="foo", last_name="bar",
            additional=None
        ))

        self.assertEqual(field.get_value_from_instance(instance), {
            'first_name': "foo",
            'last_name': "bar",
            'additional': {}
        })
 def test_get_mapping(self):
     field = ListField(IntegerField(attr='foo.bar'))
     self.assertEqual({
         'type': 'integer',
     }, field.to_dict())