本文整理汇总了Python中tests.models.Band.to_json方法的典型用法代码示例。如果您正苦于以下问题:Python Band.to_json方法的具体用法?Python Band.to_json怎么用?Python Band.to_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tests.models.Band
的用法示例。
在下文中一共展示了Band.to_json方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_saved_items_with_non_db_relation
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import to_json [as 别名]
def test_saved_items_with_non_db_relation(self):
class BandForm(ClusterForm):
class Meta:
model = Band
fields = ['name']
first_song = Song(name='I Saw Her Standing There')
second_song = Song(name='Misery')
album = Album(name='Please Please Me', songs=[first_song, second_song])
beatles = Band(name='The Beatles', albums=[album])
beatles.save()
# pack and unpack the record so that we're working with a non-db-backed queryset
new_beatles = Band.from_json(beatles.to_json())
form = BandForm({
'name': 'The Beatles',
'members-TOTAL_FORMS': 0,
'members-INITIAL_FORMS': 0,
'members-MAX_NUM_FORMS': 1000,
'albums-TOTAL_FORMS': 1,
'albums-INITIAL_FORMS': 1,
'albums-MAX_NUM_FORMS': 1000,
'albums-0-name': album.name,
'albums-0-id': album.id,
'albums-0-ORDER': 1,
'albums-0-songs-TOTAL_FORMS': 4,
'albums-0-songs-INITIAL_FORMS': 2,
'albums-0-songs-MAX_NUM_FORMS': 1000,
'albums-0-songs-0-name': first_song.name,
'albums-0-songs-0-DELETE': 'albums-0-songs-0-DELETE',
'albums-0-songs-0-id': first_song.id,
'albums-0-songs-1-name': second_song.name,
'albums-0-songs-1-id': second_song.id,
'albums-0-songs-2-name': 'Anna',
'albums-0-songs-2-id': '',
'albums-0-songs-3-name': '',
'albums-0-songs-3-id': '',
}, instance=new_beatles)
self.assertTrue(form.is_valid())
form.save()
self.assertTrue(Song.objects.filter(name='Anna').exists())
self.assertFalse(Song.objects.filter(name='I Saw Her Standing There').exists())
示例2: test_serialize_json_with_dates
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import to_json [as 别名]
def test_serialize_json_with_dates(self):
beatles = Band(name='The Beatles', members=[
BandMember(name='John Lennon'),
BandMember(name='Paul McCartney'),
], albums=[
Album(name='Rubber Soul', release_date=datetime.date(1965, 12, 3))
])
beatles_json = beatles.to_json()
self.assertTrue("John Lennon" in beatles_json)
self.assertTrue("1965-12-03" in beatles_json)
unpacked_beatles = Band.from_json(beatles_json)
self.assertEqual(datetime.date(1965, 12, 3), unpacked_beatles.albums.all()[0].release_date)