• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python models.Band类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中tests.models.Band的典型用法代码示例。如果您正苦于以下问题:Python Band类的具体用法?Python Band怎么用?Python Band使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Band类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_child_updates_without_ids

    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())
开发者ID:torchbox,项目名称:django-modelcluster,代码行数:35,代码来源:test_formset.py


示例2: test_child_updates_without_ids

    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())
开发者ID:sprymix,项目名称:django-modelcluster,代码行数:27,代码来源:test_formset.py


示例3: test_serialize

    def test_serialize(self):
        beatles = Band(name='The Beatles', members=[
            BandMember(name='John Lennon'),
            BandMember(name='Paul McCartney'),
        ])

        expected = {'pk': None, 'albums': [], 'name': 'The Beatles', 'members': [{'pk': None, 'name': 'John Lennon', 'band': None}, {'pk': None, 'name': 'Paul McCartney', 'band': None}]}
        self.assertEqual(expected, beatles.serializable_data())
开发者ID:theju,项目名称:django-modelcluster,代码行数:8,代码来源:test_serialize.py


示例4: test_save_commit_false

    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())
开发者ID:torchbox,项目名称:django-modelcluster,代码行数:58,代码来源:test_formset.py


示例5: test_incoming_form_data

    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())
开发者ID:torchbox,项目名称:django-modelcluster,代码行数:58,代码来源:test_cluster_form.py


示例6: test_can_only_commit_on_saved_parent

    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()
开发者ID:artursmet,项目名称:django-modelcluster,代码行数:9,代码来源:test_cluster.py


示例7: test_saved_items

    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())
开发者ID:torchbox,项目名称:django-modelcluster,代码行数:54,代码来源:test_cluster_form.py


示例8: test_save_commit_false

    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())
开发者ID:sprymix,项目名称:django-modelcluster,代码行数:54,代码来源:test_formset.py


示例9: test_ignore_validation_on_deleted_items

    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())
开发者ID:DjangoBD,项目名称:django-modelcluster,代码行数:53,代码来源:test_cluster_form.py


示例10: test_saved_items_with_non_db_relation

    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())
开发者ID:torchbox,项目名称:django-modelcluster,代码行数:52,代码来源:test_cluster_form.py


示例11: test_serialize_json_with_dates

    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)
开发者ID:theju,项目名称:django-modelcluster,代码行数:13,代码来源:test_serialize.py


示例12: test_deserialize

    def test_deserialize(self):
        beatles = Band.from_serializable_data({
            'pk': 9,
            'albums': [],
            'name': 'The Beatles',
            'members': [
                {'pk': None, 'name': 'John Lennon', 'band': None},
                {'pk': None, 'name': 'Paul McCartney', 'band': None},
            ]
        })
        self.assertEqual(9, beatles.id)
        self.assertEqual('The Beatles', beatles.name)
        self.assertEqual(2, beatles.members.count())
        self.assertEqual(BandMember, beatles.members.all()[0].__class__)

        authors = {}
        categories = {}
        for ii in range(1, 6):
            authors[ii] = Author.objects.create(name="Author " + str(ii))
            categories[ii] = Category.objects.create(name="Category " + str(ii))

        article = Article.from_serializable_data({
            'pk': 1,
            'title': 'Article Title 1',
            'authors': [authors[1].pk, authors[2].pk],
            'categories': [categories[2].pk, categories[3].pk, categories[4].pk]
        })
        self.assertEqual(article.id, 1)
        self.assertEqual(article.title, 'Article Title 1')
        self.assertEqual(article.authors.count(), 2)
        self.assertEqual([author.name for author in article.authors.all()],
                         ['Author 1', 'Author 2'])
        self.assertEqual(article.categories.count(), 3)
开发者ID:theju,项目名称:django-modelcluster,代码行数:33,代码来源:test_serialize.py


示例13: test_deserialize_with_sort_order

    def test_deserialize_with_sort_order(self):
        beatles = Band.from_json('{"pk": null, "albums": [{"pk": null, "name": "With The Beatles", "sort_order": 2}, {"pk": null, "name": "Please Please Me", "sort_order": 1}], "name": "The Beatles", "members": []}')
        self.assertEqual(2, beatles.albums.count())

        # Make sure the albums were ordered correctly
        self.assertEqual("Please Please Me", beatles.albums.all()[0].name)
        self.assertEqual("With The Beatles", beatles.albums.all()[1].name)
开发者ID:theju,项目名称:django-modelcluster,代码行数:7,代码来源:test_serialize.py


示例14: test_save_commit_true

    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)
开发者ID:sprymix,项目名称:django-modelcluster,代码行数:44,代码来源:test_formset.py


示例15: test_deserialize_with_multiple_sort_order

    def test_deserialize_with_multiple_sort_order(self):
        Album._meta.ordering = ['sort_order', 'name']
        beatles = Band.from_json('{"pk": null, "albums": [{"pk": 1, "name": "With The Beatles", "sort_order": 1}, {"pk": 2, "name": "Please Please Me", "sort_order": 1}, {"pk": 3, "name": "Please Please Me", "sort_order": 2}], "name": "The Beatles", "members": []}')
        Album._meta.ordering = ['sort_order']
        self.assertEqual(3, beatles.albums.count())

        # Make sure the albums were ordered correctly
        self.assertEqual(2, beatles.albums.all()[0].pk)
        self.assertEqual(1, beatles.albums.all()[1].pk)
        self.assertEqual(3, beatles.albums.all()[2].pk)
开发者ID:theju,项目名称:django-modelcluster,代码行数:10,代码来源:test_serialize.py


示例16: test_can_create_cluster

    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())
开发者ID:simudream,项目名称:django-modelcluster,代码行数:57,代码来源:test_cluster.py


示例17: test_cluster_form_without_formsets

    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())
开发者ID:torchbox,项目名称:django-modelcluster,代码行数:21,代码来源:test_cluster_form.py


示例18: test_deserialize

 def test_deserialize(self):
     beatles = Band.from_serializable_data({
         'pk': 9,
         'albums': [],
         'name': 'The Beatles',
         'members': [
             {'pk': None, 'name': 'John Lennon', 'band': None},
             {'pk': None, 'name': 'Paul McCartney', 'band': None},
         ]
     })
     self.assertEqual(9, beatles.id)
     self.assertEqual('The Beatles', beatles.name)
     self.assertEqual(2, beatles.members.count())
     self.assertEqual(BandMember, beatles.members.all()[0].__class__)
开发者ID:mikedingjan,项目名称:django-modelcluster,代码行数:14,代码来源:test_serialize.py


示例19: test_queryset_filtering

    def test_queryset_filtering(self):
        beatles = Band(name='The Beatles', members=[
            BandMember(id=1, name='John Lennon'),
            BandMember(id=2, name='Paul McCartney'),
        ])
        self.assertEqual('Paul McCartney', beatles.members.get(id=2).name)
        self.assertEqual('Paul McCartney', beatles.members.get(id='2').name)
        self.assertEqual(1, beatles.members.filter(name='Paul McCartney').count())

        # also need to be able to filter on foreign fields that return a model instance
        # rather than a simple python value
        self.assertEqual(2, beatles.members.filter(band=beatles).count())
        # and ensure that the comparison is not treating all unsaved instances as identical
        rutles = Band(name='The Rutles')
        self.assertEqual(0, beatles.members.filter(band=rutles).count())

        # and the comparison must be on the model instance's ID where available,
        # not by reference
        beatles.save()
        beatles.members.add(BandMember(id=3, name='George Harrison'))  # modify the relation so that we're not to a plain database-backed queryset

        also_beatles = Band.objects.get(id=beatles.id)
        self.assertEqual(3, beatles.members.filter(band=also_beatles).count())
开发者ID:artursmet,项目名称:django-modelcluster,代码行数:23,代码来源:test_cluster.py


示例20: test_model_with_zero_pk

 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')
开发者ID:torchbox,项目名称:django-modelcluster,代码行数:10,代码来源:test_cluster.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python models.ForeignKeyTarget类代码示例发布时间:2022-05-27
下一篇:
Python mockserver.MockServer类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap