本文整理汇总了Python中tests.models.Band.members方法的典型用法代码示例。如果您正苦于以下问题:Python Band.members方法的具体用法?Python Band.members怎么用?Python Band.members使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tests.models.Band
的用法示例。
在下文中一共展示了Band.members方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_can_create_cluster
# 需要导入模块: from tests.models import Band [as 别名]
# 或者: from tests.models.Band import members [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())