使用 django 管理操作将 csv 转换为 json
Convert csv to json with django admin actions
在 django-admin 中注册一个 csv 并通过 django-admin 中的一个操作转换为 json 并将值存储在 JSONField
中
但是,在操作 django-admin 时我遇到了这个错误,我无法将其转换为 json...
admin.py
(....)
def read_data_csv(path):
with open(path, newline='') as csvfile:
reader = csv.DictReader(csvfile)
data = []
for row in reader:
data.append(dict(row))
return data
def convert(modeladmin, request, queryset):
for extraction in queryset:
csv_file_path = extraction.lawsuits
read_data_csv(csv_file_path)
错误:
TypeError at /admin/core/extraction/
expected str, bytes or os.PathLike object, not FieldFile
这是关于 extraction.lawsuits
的,它是一个 FieldFile 实例。只需将 read_data_csv
函数 csv_file_path.path
作为参数传递。应该可以。
在 django-admin 中注册一个 csv 并通过 django-admin 中的一个操作转换为 json 并将值存储在 JSONField
中但是,在操作 django-admin 时我遇到了这个错误,我无法将其转换为 json...
admin.py
(....)
def read_data_csv(path):
with open(path, newline='') as csvfile:
reader = csv.DictReader(csvfile)
data = []
for row in reader:
data.append(dict(row))
return data
def convert(modeladmin, request, queryset):
for extraction in queryset:
csv_file_path = extraction.lawsuits
read_data_csv(csv_file_path)
错误:
TypeError at /admin/core/extraction/
expected str, bytes or os.PathLike object, not FieldFile
这是关于 extraction.lawsuits
的,它是一个 FieldFile 实例。只需将 read_data_csv
函数 csv_file_path.path
作为参数传递。应该可以。