Django中Course_id的无效关键字参数

时间:2018-07-27 04:04:33

标签: django django-models django-1.11

我正在尝试用数据填充models.table,但是出现错误

TypeError: 'course_id' is an invalid keyword argument for this function

models.py

class Course(TimeStampedModel, models.Model):
    course_id = models.AutoField(primary_key=True),
    title = models.CharField(verbose_name=_(u"title"), max_length=40, blank=True, null=True)

数据示例(数据框==)

Course Code title   
719176      lorem ipsu #1
719177      lorem ipsu #2

脚本

for i in range(0, len(df1)):
    all_courses_ids = [l.course_id for l in Course.objects.all()]
    if( df1.iloc[i]['Course Code'] not in all_courses_ids):
        Course.objects.create(
        course_id=df1.iloc[i]['Course Code']
    )

错误

<ipython-input-2-0a2a41ded7eb> in <module>()
      3     if( df1.iloc[i]['Course Code'] not in all_courses_ids):
      4         Course.objects.create(
----> 5         course_id=df1.iloc[i]['Course Code']
      6     )
      7
...
...
TypeError: 'course_id' is an invalid keyword argument for this function

1 个答案:

答案 0 :(得分:2)

您的代码中有错别字:

course_id = models.AutoField(primary_key=True),

您应该删除逗号。否则,Python会将course_id视为元组,而不是模型的字段。