TypeError: Teacher() got an unexpected keyword argument 'nickname'
TypeError: Teacher() got an unexpected keyword argument 'nickname'
TypeError: Teacher() got an unexpected keyword argument 'nickname'
2019-04-25
class Teacher(models.Model): """讲师信息表""" nickname = models.CharField(max_length=30, primary_key=True, db_index=True, verbose_name="昵称") introduction = models.TextField(default="这位教师很懒,什么都没有留下。", verbose_name="简介") fans = models.PositiveIntegerField(default='0', verbose_name="粉丝数") #非负整数类型 created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间") updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
import os import sys import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "imooc.settings") from datetime import date import random project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(project_path) # 将项目的路径添加到系统搜寻路径当中 os.environ['DJANGO_SETTINGS_MODEL'] = 'imooc.settings' # 设置项目配置文件 django.setup() # 导入模型类 from courses.models import Teacher, Course, Student, TeacherAssistant def import_data(): """使用Django-ORM导入数据""" # 讲师数据导入 create() Teacher.objects.create(nickname="Jack", introduction="Python工程师", fans=666) Teacher.objects.create(nickname="Allen", introduction="Java工程师", fans=325) Teacher.objects.create(nickname="Hllen", introduction="全栈工程师", fans=4654) # 课程数据导入 bluk_create() Course.objects.bluk_create([Course(teacher=Teacher.objects.get(nickname="Jack"), title=f"Python系列教程{i}", type=random.choice(0, 1, 2), price=random.randint(200, 300), volume=random.randint(100, 10000), online=date(2018, 10, 1)) for i in range(1, 5)]) Course.objects.bluk_create([Course(teacher=Teacher.objects.get(nickname="Allen"), title=f"Java系列教程{i}", type=random.choice(0, 1, 2), price=random.randint(200, 300), volume=random.randint(100, 10000), online=date(2018, 8, 10)) for i in range(1, 5)]) Course.objects.bluk_create([Course(teacher=Teacher.objects.get(nickname="Hllen"), title=f"全栈系列教程{i}", type=random.choice(0, 1, 2), price=random.randint(200, 300), volume=random.randint(100, 10000), online=date(2018, 6, 25)) for i in range(1, 5)]) # 学生数据导入 update_or_create() Student.objects.update_or_create(nickname="A", defaults={"age": random.randint(18, 28), "gender": random.choice(0, 1, 2), "stydy_time": random.randint(9, 999)}) Student.objects.update_or_create(nickname="B", defaults={"age": random.randint(18, 28), "gender": random.choice(0, 1, 2), "stydy_time": random.randint(9, 999)}) Student.objects.update_or_create(nickname="C", defaults={"age": random.randint(18, 28), "gender": random.choice(0, 1, 2), "stydy_time": random.randint(9, 999)}) # 正向添加 # 销量大于等于1000的课程 Student.objects.get(nickname="A").course.add(*Course.objects.filter(volume__gte=1000)) # 销量大于5000的课程 Student.objects.get(nickname="B").course.add(*Course.objects.filter(volume__gt=5000)) # 反向添加 # 学习时间大于等于500的同学 Course.objects.get(title="Python系列教程1").student_set.add(*Student.objects.filter(study_time__gte=500)) # 学习时间小于等于300小时 Course.objects.get(title="Python系列教程2").student_set.add(*Student.objects.filter(study_time__lte=300)) # 助教数据导入 get_or_create() TeacherAssistant.objects.get_or_create(nickname="助教1", defaults={"teacheer": Teacher.objects.get(nickname="Jack"), "hobby": "打篮球"}) TeacherAssistant.objects.get_or_create(nickname="助教2", defaults={"teacheer": Teacher.objects.get(nickname="Allen"), "hobby": "看书"}) TeacherAssistant.objects.get_or_create(nickname="助教3", defaults={"teacheer": Teacher.objects.get(nickname="Hllen"), "hobby": "旅游"}) return True if __name__ == '__main__': if import_data(): print("导入数据成功!")
举报