python - 设置空数组列表 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)
解决方案
你可以跳过你的 auto_id_field
value = Person(
covid_id=data[1],
firstname=data[2],
middle=data[3],
lastname=data[4],
extension=data[5]
)
推荐阅读
- ssl - 如何通过 TLS 通信 graylog 和 rsyslog
- linux - VirtualBox 上的 CentOs7:可以 Ping IP 地址但不能 Ping 网站
- swift - Swift:尽管在 for 循环中进行了赋值,但所有类实例的布尔值都保持不变
- excel - VBA Err.Raise 函数:“继续”按钮变灰
- php - 在 Apple M1 上运行 Laravel
- google-sheets - 我需要一个谷歌公式来计算在一个范围内满足两个条件的单元格
- reactjs - 具有多个参数的 Gatsby 客户端路由
- python - plotly choropleth 显示地图的一半?
- node.js - 渲染一个 mixin Pug 模板
- python - 使用 python 解析从 Javascript 呈现的网页中抓取的数据