Python mongoengine.fields.ReferenceField() Examples

The following are 10 code examples of mongoengine.fields.ReferenceField(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mongoengine.fields , or try the search function .
Example #1
Source File: test_reference.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_references(self):
        class TestSerializer(DocumentSerializer):
            class Meta:
                model = RefFieldsDoc
                fields = '__all__'

        # order is broken
        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                ref_list = ListField(child=ReferenceField(queryset=ReferencedDoc.objects, required=False), required=False)
                ref = ReferenceField(queryset=ReferencedDoc.objects, required=False)
                dbref = ReferenceField(queryset=ReferencedDoc.objects, required=False)
                cached = ReferenceField(queryset=ReferencedDoc.objects, required=False)
                generic = GenericReferenceField(required=False)
        """)
        assert repr(TestSerializer()) == expected 
Example #2
Source File: test_reference.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_recursive(self):
        class TestSerializer(DocumentSerializer):
            class Meta:
                model = RecursiveReferencingDoc
                fields = '__all__'
                depth = 3

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                ref = NestedSerializer(read_only=True):
                    id = ObjectIdField(read_only=True)
                    ref = NestedSerializer(read_only=True):
                        id = ObjectIdField(read_only=True)
                        ref = NestedSerializer(read_only=True):
                            id = ObjectIdField(read_only=True)
                            ref = ReferenceField(queryset=RecursiveReferencingDoc.objects, required=False)
        """)
        assert repr(TestSerializer()) == expected 
Example #3
Source File: test_reference.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_custom_field(self):
        class CustomReferencing(ReferenceField):
            pass

        class TestSerializer(DocumentSerializer):
            serializer_reference_field = CustomReferencing

            class Meta:
                model = ReferencingDoc
                fields = '__all__'
                depth = 0

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                ref = ReferenceField(queryset=ReferencedDoc.objects, required=False)
        """)
        assert repr(TestSerializer()) == expected 
Example #4
Source File: test_reference.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_custom_display_value(self):
        class TestField(ReferenceField):
            def display_value(self, instance):
                return 'My %s Color' % (instance.name)

        class TestSerializer(DocumentSerializer):
            color = TestField(queryset=DisplayableReferencedModel.objects.all())

            class Meta:
                model = DisplayableReferencingModel
                fields = '__all__'

        serializer = TestSerializer()
        expected = OrderedDict([(self.ids[0], 'My Red Color'),
                                (self.ids[1], 'My Green Color'),
                                (self.ids[2], 'My Blue Color')])
        assert serializer.fields['color'].choices == expected 
Example #5
Source File: test_reference.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_init_with_model(self):
        ReferenceField(ReferencedDoc) 
Example #6
Source File: test_reference.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_init_with_queryset(self):
        ReferenceField(queryset=ReferencedDoc.objects.all()) 
Example #7
Source File: test_reference.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_input(self):
        field = ReferenceField(ReferencedDoc)
        instance = ReferencedDoc.objects.create(name="foo")
        ref = instance.to_dbref()
        assert field.to_internal_value(str(instance.id)) == ref
        assert field.to_internal_value({'_id': str(instance.id)}) == ref 
Example #8
Source File: test_reference.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_output(self):
        field = ReferenceField(ReferencedDoc)
        instance = ReferencedDoc.objects.create(name="foo")
        strid = str(instance.id)
        ref = instance.to_dbref()
        assert field.to_representation(instance) == strid
        assert field.to_representation(ref) == strid 
Example #9
Source File: test_reference.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_input_other(self):
        field = ReferenceField(OtherReferencedDoc)
        instance = OtherReferencedDoc.objects.create(name="foo")
        ref = instance.to_dbref()
        assert field.to_internal_value(str(instance.id)) == ref
        assert field.to_internal_value({'_id': str(instance.id)}) == ref 
Example #10
Source File: test_reference.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_shallow(self):
        class TestSerializer(DocumentSerializer):
            class Meta:
                model = ReferencingDoc
                fields = '__all__'
                depth = 0

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                ref = ReferenceField(queryset=ReferencedDoc.objects, required=False)
        """)
        assert repr(TestSerializer()) == expected