当前位置: 首页>>代码示例>>Python>>正文


Python Bundle.data["fk"]方法代码示例

本文整理汇总了Python中tastypie.bundle.Bundle.data["fk"]方法的典型用法代码示例。如果您正苦于以下问题:Python Bundle.data["fk"]方法的具体用法?Python Bundle.data["fk"]怎么用?Python Bundle.data["fk"]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tastypie.bundle.Bundle的用法示例。


在下文中一共展示了Bundle.data["fk"]方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_hydrate

# 需要导入模块: from tastypie.bundle import Bundle [as 别名]
# 或者: from tastypie.bundle.Bundle import data["fk"] [as 别名]
    def test_hydrate(self):
        note = Note.objects.get(pk=1)
        bundle = Bundle(obj=note)

        # With no value or nullable, we should get an ``ApiFieldError``.
        field_1 = ForeignKey(UserResource, "author")
        self.assertRaises(ApiFieldError, field_1.hydrate, bundle)

        # The nullable case.
        field_2 = ForeignKey(UserResource, "author", null=True)
        field_2.instance_name = "fk"
        bundle.data["fk"] = None
        self.assertEqual(field_2.hydrate(bundle), None)

        # Wrong resource URI.
        field_3 = ForeignKey(UserResource, "author")
        field_3.instance_name = "fk"
        bundle.data["fk"] = "/api/v1/users/abc/"
        self.assertRaises(NotFound, field_3.hydrate, bundle)

        # A real, live attribute!
        field_4 = ForeignKey(UserResource, "author")
        field_4.instance_name = "fk"
        bundle.data["fk"] = "/api/v1/users/1/"
        fk_bundle = field_4.hydrate(bundle)
        self.assertEqual(fk_bundle.data["username"], u"johndoe")
        self.assertEqual(fk_bundle.data["email"], u"[email protected]")
        self.assertEqual(fk_bundle.obj.username, u"johndoe")
        self.assertEqual(fk_bundle.obj.email, u"[email protected]")

        field_5 = ForeignKey(UserResource, "author")
        field_5.instance_name = "fk"
        bundle.data["fk"] = {"username": u"mistersmith", "email": u"[email protected]", "password": u"foobar"}
        fk_bundle = field_5.hydrate(bundle)
        self.assertEqual(fk_bundle.data["username"], u"mistersmith")
        self.assertEqual(fk_bundle.data["email"], u"[email protected]")
        self.assertEqual(fk_bundle.obj.username, u"mistersmith")
        self.assertEqual(fk_bundle.obj.email, u"[email protected]")

        # Regression - Make sure Unicode keys get converted to regular strings
        #              so that we can **kwargs them.
        field_6 = ForeignKey(UserResource, "author")
        field_6.instance_name = "fk"
        bundle.data["fk"] = {u"username": u"mistersmith", u"email": u"[email protected]", u"password": u"foobar"}
        fk_bundle = field_6.hydrate(bundle)
        self.assertEqual(fk_bundle.data["username"], u"mistersmith")
        self.assertEqual(fk_bundle.data["email"], u"[email protected]")
        self.assertEqual(fk_bundle.obj.username, u"mistersmith")
        self.assertEqual(fk_bundle.obj.email, u"[email protected]")
开发者ID:aino,项目名称:django-tastypie,代码行数:51,代码来源:fields.py


注:本文中的tastypie.bundle.Bundle.data["fk"]方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。