本文整理汇总了Python中models.Employee.last_name方法的典型用法代码示例。如果您正苦于以下问题:Python Employee.last_name方法的具体用法?Python Employee.last_name怎么用?Python Employee.last_name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Employee
的用法示例。
在下文中一共展示了Employee.last_name方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_custom_pk
# 需要导入模块: from models import Employee [as 别名]
# 或者: from models.Employee import last_name [as 别名]
def test_custom_pk(self):
dan = Employee(employee_code=123, first_name='Dan', last_name='Jones')
dan.save()
self.assertQuerysetEqual(Employee.objects.all(),
['<Employee: Dan Jones>'])
fran = Employee(employee_code=456, first_name='Fran', last_name='Bones')
fran.save()
self.assertQuerysetEqual(Employee.objects.all(),
['<Employee: Fran Bones>', '<Employee: Dan Jones>'])
self.assertEqual(repr(Employee.objects.get(pk=123)),
'<Employee: Dan Jones>')
self.assertEqual(repr(Employee.objects.get(pk=456)),
'<Employee: Fran Bones>')
self.assertRaises(Employee.DoesNotExist,
Employee.objects.get, pk=42)
# Use the name of the primary key, rather than pk.
self.assertEqual(repr(Employee.objects.get(employee_code__exact=123)),
'<Employee: Dan Jones>')
# pk can be used as a substitute for the primary key.
self.assertQuerysetEqual(Employee.objects.filter(pk__in=[123, 456]),
['<Employee: Fran Bones>', '<Employee: Dan Jones>'])
# The primary key can be accessed via the pk property on the model.
e = Employee.objects.get(pk=123)
self.assertEqual(e.pk, 123)
# Or we can use the real attribute name for the primary key:
self.assertEqual(e.employee_code, 123)
# Fran got married and changed her last name.
fran = Employee.objects.get(pk=456)
fran.last_name = 'Jones'
fran.save()
self.assertQuerysetEqual(Employee.objects.filter(last_name__exact='Jones') ,
['<Employee: Dan Jones>', '<Employee: Fran Jones>'])
emps = Employee.objects.in_bulk([123, 456])
self.assertEqual(repr(emps[123]),
'<Employee: Dan Jones>')
b = Business(name='Sears')
b.save()
b.employees.add(dan, fran)
self.assertQuerysetEqual(b.employees.all(),
['<Employee: Dan Jones>', '<Employee: Fran Jones>'])
self.assertQuerysetEqual(fran.business_set.all(),
['<Business: Sears>'])
self.assertEqual(repr(Business.objects.in_bulk(['Sears'])),
"{u'Sears': <Business: Sears>}")
self.assertQuerysetEqual(Business.objects.filter(name__exact='Sears'),
['<Business: Sears>'])
self.assertQuerysetEqual(Business.objects.filter(pk='Sears'),
['<Business: Sears>'])
# Queries across tables, involving primary key
self.assertQuerysetEqual(Employee.objects.filter(business__name__exact='Sears'),
['<Employee: Dan Jones>', '<Employee: Fran Jones>'])
self.assertQuerysetEqual(Employee.objects.filter(business__pk='Sears'),
['<Employee: Dan Jones>', '<Employee: Fran Jones>'])
self.assertQuerysetEqual(Business.objects.filter(employees__employee_code__exact=123),
['<Business: Sears>'])
self.assertQuerysetEqual(Business.objects.filter(employees__pk=123),
['<Business: Sears>'])
self.assertQuerysetEqual(Business.objects.filter(employees__first_name__startswith='Fran'),
['<Business: Sears>'])