本文整理汇总了Python中tastypie.serializers.Serializer.to_json方法的典型用法代码示例。如果您正苦于以下问题:Python Serializer.to_json方法的具体用法?Python Serializer.to_json怎么用?Python Serializer.to_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tastypie.serializers.Serializer
的用法示例。
在下文中一共展示了Serializer.to_json方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: index
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def index(request):
"""
Page shell for the client-side application.
Bootstraps read-once data onto the page.
"""
serializer = Serializer()
cr = CategoryResource()
categories = list(Category.objects.annotate(dataset_count=Count('datasets')))
bundles = [cr.build_bundle(obj=c) for c in categories]
categories_bootstrap = [cr.full_dehydrate(b) for b in bundles]
uncategorized = Category(
id=settings.PANDA_UNCATEGORIZED_ID,
slug=settings.PANDA_UNCATEGORIZED_SLUG,
name=settings.PANDA_UNCATEGORIZED_NAME)
uncategorized.__dict__['dataset_count'] = Dataset.objects.filter(categories=None).count()
uncategorized_bundle = cr.full_dehydrate(cr.build_bundle(obj=uncategorized))
categories_bootstrap.append(uncategorized_bundle)
return render_to_response('index.html', {
'settings': settings,
'max_upload_size': int(config_value('MISC', 'MAX_UPLOAD_SIZE')),
'email_enabled': int(config_value('EMAIL', 'EMAIL_ENABLED')),
'demo_mode_enabled': int(config_value('MISC', 'DEMO_MODE_ENABLED')),
'bootstrap_data': serializer.to_json({
'categories': categories_bootstrap
}),
'moment_lang_code': settings.MOMENT_LANGUAGE_MAPPING.get(settings.LANGUAGE_CODE, None),
})
示例2: index
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def index(request):
"""
Page shell for the client-side application.
Bootstraps read-once data onto the page.
"""
serializer = Serializer()
cr = CategoryResource()
categories = list(Category.objects.annotate(dataset_count=Count('datasets')))
bundles = [cr.build_bundle(obj=c) for c in categories]
categories_bootstrap = [cr.full_dehydrate(b) for b in bundles]
uncategorized = Category(
id=settings.PANDA_UNCATEGORIZED_ID,
slug=settings.PANDA_UNCATEGORIZED_SLUG,
name=settings.PANDA_UNCATEGORIZED_NAME)
uncategorized.__dict__['dataset_count'] = Dataset.objects.filter(categories=None).count()
uncategorized_bundle = cr.full_dehydrate(cr.build_bundle(obj=uncategorized))
categories_bootstrap.append(uncategorized_bundle)
return render_to_response('index.html', {
'settings': settings,
'demo_mode': int(config_value('MISC', 'DEMO_MODE')),
'bootstrap_data': serializer.to_json({
'categories': categories_bootstrap
})
})
示例3: test_to_json_decimal_list_dict
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def test_to_json_decimal_list_dict(self):
serializer = Serializer()
resource = self.another_obj_list[0]
self.assertEqual(
serializer.to_json(resource),
'{"aliases": ["Mr. Smith", "John Doe"], "content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": "1", "is_active": true, "meta": {"threat": "high"}, "owed": "102.57", "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}',
)
示例4: test_to_json_single
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def test_to_json_single(self):
serializer = Serializer()
resource = self.obj_list[0]
self.assertEqual(
serializer.to_json(resource),
'{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": "1", "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}',
)
示例5: test_to_json_nested
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def test_to_json_nested(self):
serializer = Serializer()
resource = self.obj_list[0]
data = {"stuff": {"foo": "bar", "object": resource}}
self.assertEqual(
serializer.to_json(data),
'{"stuff": {"foo": "bar", "object": {"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": "1", "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}}}',
)
示例6: test_to_jsonp
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def test_to_jsonp(self):
serializer = Serializer()
sample_1 = self.get_sample1()
options = {'callback': 'myCallback'}
serialized = serializer.to_jsonp(sample_1, options=options)
serialized_json = serializer.to_json(sample_1)
self.assertEqual('myCallback(%s)' % serialized_json,
serialized)
示例7: test_to_json_nested
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def test_to_json_nested(self):
serializer = Serializer()
representation = NoteRepresentation.get_list()[0]
data = {
'stuff': {
'foo': 'bar',
'object': representation,
}
}
self.assertEqual(serializer.to_json(data), '{"stuff": {"foo": "bar", "object": {"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "Tue, 30 Mar 2010 20:05:00 -0500", "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "Tue, 30 Mar 2010 20:05:00 -0500"}}}')
示例8: login
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def login(self, username, password, client=None):
login_url = reverse('api_login', kwargs={'resource_name':'users', 'api_name':'v1'})
credentials = {"username": username, "password": password}
resp = None
if type(client) is Client:
serializer = Serializer()
data = serializer.to_json(credentials)
resp = client.post(login_url, data=data, content_type="application/json")
else:
resp = self.api_client.post(login_url, format="json", data=credentials)
return resp
示例9: example_data
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def example_data(request, resource_name, api):
tastyfactory = TastyFactory(api)
resource_mockup = tastyfactory[resource_name]
with test_db(verbosity=0):
post_data = resource_mockup.create_post_data()
get_data = resource_mockup.create_get_data()
serializer = Serializer()
json_string = serializer.to_json({
'POST': post_data,
'GET': get_data
})
return HttpResponse(json_string, mimetype="application/json; charset=utf-8")
示例10: index
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def index(request):
"""
Page shell for the client-side application.
Bootstraps read-once data onto the page.
"""
serializer = Serializer()
resource = ActiveCallResource()
since = datetime.now() - timedelta(hours=settings.DEFAULT_HOURS_DISPLAYED)
calls = ActiveCall.objects.filter(reported__gt=since)
bundles = [resource.build_bundle(obj=c) for c in calls]
calls_bootstrap = [resource.full_dehydrate(b) for b in bundles]
return render_to_response('index.html', {
'settings': settings,
'bootstrap_data': serializer.to_json({
'active_calls': calls_bootstrap
})
})
示例11: test_to_json_multirepr
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def test_to_json_multirepr(self):
serializer = Serializer()
self.assertEqual(serializer.to_json(self.obj_list), '[{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": "1", "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}, {"content": "The dog ate my cat today. He looks seriously uncomfortable.", "created": "2010-03-31T20:05:00", "id": "2", "is_active": true, "resource_uri": "", "slug": "another-post", "title": "Another Post", "updated": "2010-03-31T20:05:00"}, {"content": "My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.", "created": "2010-04-01T20:05:00", "id": "4", "is_active": true, "resource_uri": "", "slug": "recent-volcanic-activity", "title": "Recent Volcanic Activity.", "updated": "2010-04-01T20:05:00"}, {"content": "Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!", "created": "2010-04-02T10:05:00", "id": "6", "is_active": true, "resource_uri": "", "slug": "grannys-gone", "title": "Granny\'s Gone", "updated": "2010-04-02T10:05:00"}]')
示例12: test_round_trip_json
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def test_round_trip_json(self):
serializer = Serializer()
sample_data = self.get_sample2()
serialized = serializer.to_json(sample_data)
unserialized = serializer.from_json(serialized)
self.assertEqual(sample_data, unserialized)
示例13: test_to_json
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def test_to_json(self):
serializer = Serializer()
sample_1 = self.get_sample1()
self.assertEqual(serializer.to_json(sample_1), '{"age": 27, "date_joined": "2010-03-27", "name": "Daniel"}')
示例14: test_to_json_single
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def test_to_json_single(self):
serializer = Serializer()
representation = NoteRepresentation.get_list()[0]
self.assertEqual(serializer.to_json(representation), '{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "Tue, 30 Mar 2010 20:05:00 -0500", "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "Tue, 30 Mar 2010 20:05:00 -0500"}')
示例15: to_html
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import to_json [as 别名]
def to_html(self, data, options=None):
return Serializer.to_json(self, data, options=options) # RICK EDIT