首页 > 解决方案 > 设置空数组列表 Django

问题描述

如何设置我的数组列表的 null 或空值以插入 id autoincrement ,我已经尝试过 'None' 但它返回错误。谁能帮我?

错误元组索引必须是整数或切片,而不是 NoneType

视图.py

def upload_excel_file(request):
    if request.method =='POST':
        person_resource = PersonResource()
        dataset = Dataset()
        new_person = request.FILES['myfile']

        if not new_person.name.endswith('xlsx'):
            messages.info(request,'wrong format')
            return render (request,'dashboard.html')
        imported_data = dataset.load(new_person.read(),format="xlsx")
        for data in imported_data:
            value = Person(
                data[None],  // It should Empty value 
                data[1],
                data[2],
                data[3],
                data[4],
                data[5]
            )
            value.save()
    return render(request,'dashboard.html')

模型.py

class Person(models.Model):
   person_id = models.AutoField(primary_key=True)
   covid_id = models.CharField(max_length=50)
   firstname = models.CharField(max_length=50)
   middle = models.CharField(max_length=50)
   lastname   = models.CharField(max_length=50,blank=True)
   extension  = models.CharField(max_length=50)

标签: pythondjango

解决方案


你可以跳过你的 auto_id_field

value = Person(
                covid_id=data[1],
                firstname=data[2],
                middle=data[3],
                lastname=data[4],
                extension=data[5]
            )

推荐阅读