TypeError: Teacher() got an unexpected keyword argument 'nickname'

来源:5-2 Django数据导入

Bleach丶MR

2019-04-25 10:53

TypeError: Teacher() got an unexpected keyword argument 'nickname'

写回答 关注

5回答

  • Jack
    2019-04-25 14:40:25

    卧槽。。提交错地方了··

    Bleach... 回复Jack

    单词没有写错,但是数据看显示的id这个字段,截了图在下面,麻烦老师看下

    2019-04-26 09:48:24

    共 4 条回复 >

  • Jack
    2019-04-25 14:38:27

    前面的确一直没讲到调试,后面章节视频会介绍一下如何调试。

    https://img3.mukewang.com/5cc154ce00019f1511590941.jpg

    我已经在前端,某条状态下评论了

    https://img3.mukewang.com/5cc154dc0001fc1e11240846.jpg

    那里的↑  ↓  ^ 按钮,用用就知道了,鼠标放上面也有提示

    https://img1.mukewang.com/5cc154e80001a94b11910767.jpg

    当我运行到第三个断点的时候,下面就能看到post数据了,self里面的内容可以好好研究下,望采纳~

  • Bleach丶MR
    2019-04-26 09:47:20

    https://img2.mukewang.com/5cc263110001b0a714990773.jpg数据库显示是id

  • Bleach丶MR
    2019-04-26 09:46:14
    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="更新时间")


  • Bleach丶MR
    2019-04-25 15:00:45
    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("导入数据成功!")


全面掌握Django ORM

全面掌握Django ORM,以Django官方文档为依据,准确、权威。

11966 学习 · 41 问题

查看课程