本文整理汇总了Python中tests.models.Band.save方法的典型用法代码示例。如果您正苦于以下问题:Python Band.save方法的具体用法?Python Band.save怎么用?Python Band.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tests.models.Band
的用法示例。
在下文中一共展示了Band.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_child_updates_without_ids
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_child_updates_without_ids(self):
john = BandMember(name='John Lennon')
beatles = Band(name='The Beatles', members=[
john
])
beatles.save()
john_id = john.id
paul = BandMember(name='Paul McCartney')
beatles.members.add(paul)
BandMembersFormset = childformset_factory(Band, BandMember, extra=3)
band_members_formset = BandMembersFormset({
'form-TOTAL_FORMS': 2,
'form-INITIAL_FORMS': 2,
'form-MAX_NUM_FORMS': 1000,
'form-0-name': 'John Lennon',
'form-0-id': john_id,
'form-1-name': 'Paul McCartney', # NB no way to know programmatically that this form corresponds to the 'paul' object
'form-1-id': '',
}, instance=beatles)
self.assertTrue(band_members_formset.is_valid())
band_members_formset.save(commit=False)
self.assertEqual(2, beatles.members.count())
示例2: test_child_updates_without_ids
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_child_updates_without_ids(self):
first_song = Song(name='I Saw Her Standing There')
album = Album(name='Please Please Me', songs=[first_song])
beatles = Band(name='The Beatles', albums=[album])
beatles.save()
first_song_id = first_song.id
second_song = Song(name='Misery')
album.songs.add(second_song)
AlbumsFormset = childformset_factory(Band, Album, form=ClusterForm, extra=3)
albums_formset = AlbumsFormset({
'form-TOTAL_FORMS': 1,
'form-INITIAL_FORMS': 1,
'form-MAX_NUM_FORMS': 1000,
'form-0-name': 'Please Please Me',
'form-0-id': album.id,
'form-0-songs-TOTAL_FORMS': 2,
'form-0-songs-INITIAL_FORMS': 2,
'form-0-songs-MAX_NUM_FORMS': 1000,
'form-0-songs-0-name': 'I Saw Her Standing There',
'form-0-songs-0-id': first_song_id,
'form-0-songs-1-name': 'Misery',
'form-0-songs-1-id': '',
}, instance=beatles)
self.assertTrue(albums_formset.is_valid())
albums_formset.save(commit=False)
self.assertEqual(2, beatles.albums.first().songs.count())
示例3: test_incoming_form_data
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_incoming_form_data(self):
class BandForm(ClusterForm):
class Meta:
model = Band
fields = ['name']
beatles = Band(name='The Beatles', albums=[
Album(name='Please Please Me', songs=[
Song(name='I Saw Her Standing There')
]),
])
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': 0,
'albums-MAX_NUM_FORMS': 1000,
'albums-0-name': 'Please Please Me',
'albums-0-id': '',
'albums-0-ORDER': 1,
'albums-0-songs-TOTAL_FORMS': 2,
'albums-0-songs-INITIAL_FORMS': 1,
'albums-0-songs-MAX_NUM_FORMS': 1000,
'albums-0-songs-0-name': 'I Saw Her Standing There',
'albums-0-songs-0-DELETE': 'albums-0-songs-0-DELETE',
'albums-0-songs-0-id': '',
'albums-0-songs-1-name': 'Misery',
'albums-0-songs-1-id': '',
}, instance=beatles)
self.assertTrue(form.is_valid())
result = form.save(commit=False)
self.assertEqual(result, beatles)
self.assertEqual(1, beatles.albums.count())
self.assertEqual('Please Please Me', beatles.albums.first().name)
self.assertEqual(1, beatles.albums.first().songs.all().count())
self.assertEqual('Misery', beatles.albums.first().songs.first().name)
# should not exist in the database yet
self.assertFalse(Album.objects.filter(name='Please Please Me').exists())
self.assertFalse(Song.objects.filter(name='Misery').exists())
beatles.save()
# this should create database entries
self.assertTrue(Band.objects.filter(name='The Beatles').exists())
self.assertTrue(Album.objects.filter(name='Please Please Me').exists())
self.assertTrue(Song.objects.filter(name='Misery').exists())
self.assertFalse(Song.objects.filter(name='I Saw Her Standing There').exists())
示例4: test_save_commit_false
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_save_commit_false(self):
first_song = Song(name='I Saw Her Standing There')
second_song = Song(name='Mystery')
album = Album(name='Please Please Me', songs=[first_song, second_song])
beatles = Band(name='The Beatles', albums=[album])
beatles.save()
first_song_id, second_song_id = first_song.id, second_song.id
AlbumsFormset = childformset_factory(Band, Album, form=ClusterForm, extra=3)
albums_formset = AlbumsFormset({
'form-TOTAL_FORMS': 1,
'form-INITIAL_FORMS': 1,
'form-MAX_NUM_FORMS': 1000,
'form-0-name': 'Please Please Me',
'form-0-id': album.id,
'form-0-songs-TOTAL_FORMS': 4,
'form-0-songs-INITIAL_FORMS': 2,
'form-0-songs-MAX_NUM_FORMS': 1000,
'form-0-songs-0-name': 'I Saw Her Standing There',
'form-0-songs-0-DELETE': 'form-0-songs-0-DELETE',
'form-0-songs-0-id': first_song_id,
'form-0-songs-1-name': 'Misery', # changing data of an existing record
'form-0-songs-1-id': second_song_id,
'form-0-songs-2-name': '',
'form-0-songs-2-id': '',
'form-0-songs-3-name': 'Chains', # adding a record
'form-0-songs-3-id': '',
}, instance=beatles)
self.assertTrue(albums_formset.is_valid())
updated_albums = albums_formset.save(commit=False)
# updated_members should only include the items that have been changed and not deleted
self.assertEqual(1, len(updated_albums))
self.assertEqual('Please Please Me', updated_albums[0].name)
self.assertEqual(2, updated_albums[0].songs.count())
self.assertEqual('Misery', updated_albums[0].songs.first().name)
self.assertEqual(second_song_id, updated_albums[0].songs.first().id)
self.assertEqual('Chains', updated_albums[0].songs.all()[1].name)
self.assertEqual(None, updated_albums[0].songs.all()[1].id)
# Changes should not be committed to the db yet
self.assertTrue(Song.objects.filter(name='I Saw Her Standing There', id=first_song_id).exists())
self.assertEqual('Mystery', Song.objects.get(id=second_song_id).name)
self.assertFalse(Song.objects.filter(name='Chains').exists())
beatles.albums.first().songs.commit()
# this should create/update/delete database entries
self.assertEqual('Misery', Song.objects.get(id=second_song_id).name)
self.assertTrue(Song.objects.filter(name='Chains').exists())
self.assertFalse(Song.objects.filter(name='I Saw Her Standing There').exists())
示例5: test_can_create_cluster
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_can_create_cluster(self):
beatles = Band(name='The Beatles')
self.assertEqual(0, beatles.members.count())
beatles.members = [
BandMember(name='John Lennon'),
BandMember(name='Paul McCartney'),
]
# we should be able to query this relation using (some) queryset methods
self.assertEqual(2, beatles.members.count())
self.assertEqual('John Lennon', beatles.members.all()[0].name)
self.assertEqual('Paul McCartney', beatles.members.filter(name='Paul McCartney')[0].name)
self.assertEqual('Paul McCartney', beatles.members.get(name='Paul McCartney').name)
self.assertRaises(BandMember.DoesNotExist, lambda: beatles.members.get(name='Reginald Dwight'))
self.assertRaises(BandMember.MultipleObjectsReturned, lambda: beatles.members.get())
self.assertEqual([('Paul McCartney',)], beatles.members.filter(name='Paul McCartney').values_list('name'))
self.assertEqual(['Paul McCartney'], beatles.members.filter(name='Paul McCartney').values_list('name', flat=True))
# quick-and-dirty check that we can invoke values_list with empty args list
beatles.members.filter(name='Paul McCartney').values_list()
self.assertTrue(beatles.members.filter(name='Paul McCartney').exists())
self.assertFalse(beatles.members.filter(name='Reginald Dwight').exists())
self.assertEqual('John Lennon', beatles.members.first().name)
self.assertEqual('Paul McCartney', beatles.members.last().name)
self.assertTrue('John Lennon', beatles.members.order_by('name').first())
self.assertTrue('Paul McCartney', beatles.members.order_by('-name').first())
# these should not exist in the database yet
self.assertFalse(Band.objects.filter(name='The Beatles').exists())
self.assertFalse(BandMember.objects.filter(name='John Lennon').exists())
beatles.save()
# this should create database entries
self.assertTrue(Band.objects.filter(name='The Beatles').exists())
self.assertTrue(BandMember.objects.filter(name='John Lennon').exists())
john_lennon = BandMember.objects.get(name='John Lennon')
beatles.members = [john_lennon]
# reassigning should take effect on the in-memory record
self.assertEqual(1, beatles.members.count())
# but not the database
self.assertEqual(2, Band.objects.get(name='The Beatles').members.count())
beatles.save()
# now updated in the database
self.assertEqual(1, Band.objects.get(name='The Beatles').members.count())
self.assertEqual(1, BandMember.objects.filter(name='John Lennon').count())
# removed member should be deleted from the db entirely
self.assertEqual(0, BandMember.objects.filter(name='Paul McCartney').count())
# queries on beatles.members should now revert to SQL
self.assertTrue(beatles.members.extra(where=["tests_bandmember.name='John Lennon'"]).exists())
示例6: test_can_only_commit_on_saved_parent
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_can_only_commit_on_saved_parent(self):
beatles = Band(name='The Beatles', members=[
BandMember(name='John Lennon'),
BandMember(name='Paul McCartney'),
])
self.assertRaises(IntegrityError, lambda: beatles.members.commit())
beatles.save()
beatles.members.commit()
示例7: test_model_with_zero_pk
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_model_with_zero_pk(self):
beatles = Band(name='The Beatles', members=[
BandMember(name='John Lennon'),
BandMember(name='Paul McCartney'),
])
beatles.save()
beatles.pk = 0
beatles.members.commit()
beatles.save()
self.assertEqual(Band.objects.get(pk=0).name, 'The Beatles')
示例8: test_saved_items
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_saved_items(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()
self.assertTrue(album.id)
self.assertTrue(first_song.id)
self.assertTrue(second_song.id)
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=beatles)
self.assertTrue(form.is_valid())
form.save()
self.assertTrue(Song.objects.filter(name='Anna').exists())
self.assertTrue(Song.objects.filter(name='Misery').exists())
self.assertFalse(Song.objects.filter(name='I Saw Her Standing There').exists())
示例9: test_integrity_error_with_none_pk
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_integrity_error_with_none_pk(self):
beatles = Band(name='The Beatles', members=[
BandMember(name='John Lennon'),
BandMember(name='Paul McCartney'),
])
beatles.save()
beatles.pk = None
self.assertRaises(IntegrityError, lambda: beatles.members.commit())
# this should work fine, as Django will end up cloning this entity
beatles.save()
self.assertEqual(Band.objects.get(pk=beatles.pk).name, 'The Beatles')
示例10: test_save_commit_false
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_save_commit_false(self):
john = BandMember(name='John Lennon')
paul = BandMember(name='Paul McCartney')
ringo = BandMember(name='Richard Starkey')
beatles = Band(name='The Beatles', members=[
john, paul, ringo
])
beatles.save()
john_id, paul_id, ringo_id = john.id, paul.id, ringo.id
BandMembersFormset = childformset_factory(Band, BandMember, extra=3)
band_members_formset = BandMembersFormset({
'form-TOTAL_FORMS': 5,
'form-INITIAL_FORMS': 3,
'form-MAX_NUM_FORMS': 1000,
'form-0-name': 'John Lennon',
'form-0-DELETE': 'form-0-DELETE',
'form-0-id': john_id,
'form-1-name': 'Paul McCartney',
'form-1-id': paul_id,
'form-2-name': 'Ringo Starr', # changing data of an existing record
'form-2-id': ringo_id,
'form-3-name': '',
'form-3-id': '',
'form-4-name': 'George Harrison', # adding a record
'form-4-id': '',
}, instance=beatles)
self.assertTrue(band_members_formset.is_valid())
updated_members = band_members_formset.save(commit=False)
# updated_members should only include the items that have been changed and not deleted
self.assertEqual(2, len(updated_members))
self.assertEqual('Ringo Starr', updated_members[0].name)
self.assertEqual(ringo_id, updated_members[0].id)
self.assertEqual('George Harrison', updated_members[1].name)
self.assertEqual(None, updated_members[1].id)
# Changes should not be committed to the db yet
self.assertTrue(BandMember.objects.filter(name='John Lennon', id=john_id).exists())
self.assertEqual('Richard Starkey', BandMember.objects.get(id=ringo_id).name)
self.assertFalse(BandMember.objects.filter(name='George Harrison').exists())
beatles.members.commit()
# this should create/update/delete database entries
self.assertEqual('Ringo Starr', BandMember.objects.get(id=ringo_id).name)
self.assertTrue(BandMember.objects.filter(name='George Harrison').exists())
self.assertFalse(BandMember.objects.filter(name='John Lennon').exists())
示例11: test_ignore_validation_on_deleted_items
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_ignore_validation_on_deleted_items(self):
class BandForm(ClusterForm):
class Meta:
model = Band
fields = ['name']
please_please_me = Album(name='Please Please Me', release_date=datetime.date(1963, 3, 22))
beatles = Band(name='The Beatles', albums=[please_please_me])
beatles.save()
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': 'With The Beatles',
'albums-0-release_date': '1963-02-31', # invalid date
'albums-0-id': please_please_me.id,
'albums-0-ORDER': 1,
}, instance=beatles)
self.assertFalse(form.is_valid())
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': 'With The Beatles',
'albums-0-release_date': '1963-02-31', # invalid date
'albums-0-id': please_please_me.id,
'albums-0-ORDER': 1,
'albums-0-DELETE': 'albums-0-DELETE',
}, instance=beatles)
self.assertTrue(form.is_valid())
form.save(commit=False)
self.assertEqual(0, beatles.albums.count())
self.assertEqual(1, Band.objects.get(id=beatles.id).albums.count())
beatles.save()
self.assertEqual(0, Band.objects.get(id=beatles.id).albums.count())
示例12: test_saved_items_with_non_db_relation
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [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())
示例13: test_save_commit_true
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_save_commit_true(self):
john = BandMember(name='John Lennon')
paul = BandMember(name='Paul McCartney')
ringo = BandMember(name='Richard Starkey')
beatles = Band(name='The Beatles', members=[
john, paul, ringo
])
beatles.save()
john_id, paul_id, ringo_id = john.id, paul.id, ringo.id
self.assertTrue(john_id)
self.assertTrue(paul_id)
band_members_formset = self.BandMembersFormset({
'form-TOTAL_FORMS': 4,
'form-INITIAL_FORMS': 3,
'form-MAX_NUM_FORMS': 1000,
'form-0-name': 'John Lennon',
'form-0-DELETE': 'form-0-DELETE',
'form-0-id': john_id,
'form-1-name': 'Paul McCartney',
'form-1-id': paul_id,
'form-2-name': 'Ringo Starr', # changing data of an existing record
'form-2-id': ringo_id,
'form-3-name': '',
'form-3-id': '',
}, queryset=beatles.members.all())
self.assertTrue(band_members_formset.is_valid())
updated_members = band_members_formset.save()
self.assertEqual(1, len(updated_members))
self.assertEqual('Ringo Starr', updated_members[0].name)
self.assertEqual(ringo_id, updated_members[0].id)
self.assertFalse(BandMember.objects.filter(id=john_id).exists())
self.assertEqual('Paul McCartney', BandMember.objects.get(id=paul_id).name)
self.assertEqual(beatles.id, BandMember.objects.get(id=paul_id).band_id)
self.assertEqual('Ringo Starr', BandMember.objects.get(id=ringo_id).name)
self.assertEqual(beatles.id, BandMember.objects.get(id=ringo_id).band_id)
示例14: test_cluster_form_without_formsets
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_cluster_form_without_formsets(self):
class BandForm(ClusterForm):
class Meta:
model = Band
formsets = ()
fields = ['name']
beatles = Band(name='The Beatles')
beatles.save()
form = BandForm({
'name': "The New Beatles",
}, instance=beatles)
self.assertTrue(form.is_valid())
form.save(commit=False)
self.assertEqual(1, Band.objects.filter(name='The Beatles').count())
beatles.save()
self.assertEqual(0, Band.objects.filter(name='The Beatles').count())
示例15: test_save_with_update_fields
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import save [as 别名]
def test_save_with_update_fields(self):
beatles = Band(name='The Beatles', members=[
BandMember(name='John Lennon'),
BandMember(name='Paul McCartney'),
], albums=[
Album(name='Please Please Me', sort_order=1),
Album(name='With The Beatles', sort_order=2),
Album(name='Abbey Road', sort_order=3),
])
beatles.save()
# modify both relations, but only commit the change to members
beatles.members.clear()
beatles.albums.clear()
beatles.name = 'The Rutles'
beatles.save(update_fields=['name', 'members'])
updated_beatles = Band.objects.get(pk=beatles.pk)
self.assertEqual(updated_beatles.name, 'The Rutles')
self.assertEqual(updated_beatles.members.count(), 0)
self.assertEqual(updated_beatles.albums.count(), 3)