You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.3 KiB
36 lines
1.3 KiB
from orm_demo.models import * |
|
|
|
# 插入数据 |
|
# 先实例化对象,然后为对象赋值 |
|
depart_data = DepartInfo() |
|
depart_data.depart_name = '开发部' |
|
depart_data.save() |
|
|
|
# 实例化对象同时赋值 |
|
skill_data = SkillInfo(skill_name='PHP') |
|
skill_data.save() |
|
|
|
# create方法赋值 |
|
user = BaseUserInfo.objects.create(username='张三', password='zhangsan', status=1) |
|
|
|
# create方式赋值, 字典格式数据 |
|
data = {'username': '李四', 'password': 'lisi', 'status': 1} |
|
user = BaseUserInfo.objects.create(**data) |
|
|
|
# 批量插入 |
|
data_list = [ |
|
{'username': '王五', 'password': 'wangwu', 'status': 1}, |
|
{'username': '孙六', 'password': 'sunliu', 'status': 1} |
|
] |
|
user_obj_list = [] |
|
for data in data_list: |
|
user_obj = BaseUserInfo(username=data['username'], password=data['password'], status=data['status']) |
|
user_obj_list.append(user_obj) |
|
users = BaseUserInfo.objects.bulk_create(user_obj_list) |
|
|
|
# 插入外键关联的数据 |
|
user1 = BaseUserInfo.objects.filter(username='张三').first() |
|
department1 = DepartInfo.objects.filter(depart_name='开发部').first() |
|
user_e1 = UserExtendInfo(username='张三', true_name='张三丰', sex=1, age=33, department='开发部', salary='230000.00', |
|
status=1, memo='', user=user1, depart=department1) |
|
user_e1.save()
|
|
|