本文整理汇总了Python中mongoengine.fields.ReferenceField方法的典型用法代码示例。如果您正苦于以下问题:Python fields.ReferenceField方法的具体用法?Python fields.ReferenceField怎么用?Python fields.ReferenceField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongoengine.fields
的用法示例。
在下文中一共展示了fields.ReferenceField方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_references
# 需要导入模块: from mongoengine import fields [as 别名]
# 或者: from mongoengine.fields import ReferenceField [as 别名]
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
示例2: test_recursive
# 需要导入模块: from mongoengine import fields [as 别名]
# 或者: from mongoengine.fields import ReferenceField [as 别名]
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
示例3: test_custom_field
# 需要导入模块: from mongoengine import fields [as 别名]
# 或者: from mongoengine.fields import ReferenceField [as 别名]
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
示例4: test_custom_display_value
# 需要导入模块: from mongoengine import fields [as 别名]
# 或者: from mongoengine.fields import ReferenceField [as 别名]
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
示例5: test_init_with_model
# 需要导入模块: from mongoengine import fields [as 别名]
# 或者: from mongoengine.fields import ReferenceField [as 别名]
def test_init_with_model(self):
ReferenceField(ReferencedDoc)
示例6: test_init_with_queryset
# 需要导入模块: from mongoengine import fields [as 别名]
# 或者: from mongoengine.fields import ReferenceField [as 别名]
def test_init_with_queryset(self):
ReferenceField(queryset=ReferencedDoc.objects.all())
示例7: test_input
# 需要导入模块: from mongoengine import fields [as 别名]
# 或者: from mongoengine.fields import ReferenceField [as 别名]
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
示例8: test_output
# 需要导入模块: from mongoengine import fields [as 别名]
# 或者: from mongoengine.fields import ReferenceField [as 别名]
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
示例9: test_input_other
# 需要导入模块: from mongoengine import fields [as 别名]
# 或者: from mongoengine.fields import ReferenceField [as 别名]
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
示例10: test_shallow
# 需要导入模块: from mongoengine import fields [as 别名]
# 或者: from mongoengine.fields import ReferenceField [as 别名]
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