1

从 ms excel 中的一列分解文本并将其插入 Django 模型中的 2 列时,我遇到了问题。这是我的意见.py

class UploadFileForm(forms.Form):
    file = forms.FileField()

def import_data(request):
    if request.method == "POST":
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            request.FILES['file'].save_to_database(
                model=Transaksi,
                mapdict=['tanggal', 'kode','unit','keterangan', 'nominal'],

            )
            return HttpResponse("OK", status=200)
        else:
            return HttpResponseBadRequest('Gagal Upload')
    else:
        form = UploadFileForm()
        return render(request, 'kastransaksi/transaksi_formupload.html', {'form': form})

这是我的models.py

class Transaksi(models.Model):
    tanggal = models.DateField(null=True, blank=True)
    kode = models.CharField(max_length=2, null=True, blank=True)
    unit = models.CharField(max_length=2, null=True, blank=True)
    keterangan = models.CharField(max_length=100, null=True, blank=True)
    nominal = models.IntegerField(null=True, blank=True)

我的excel文件只包含三列

tangal        keterangan                                   nominal
19/01/2016    944448800010303282-SPP Raihanah Barmim       205000
19/02/2016    944448800010303282-SPP Raihanah Barmim       205000

我想将“keterangan”列中的文本拆分为我的 Django 模型字段,其中有“kode”和“unit”。怎么做?

4

1 回答 1

1

save_to_database是一种仅将 excel 表导入数据库模型的便捷方法。如果电子表格需要进一步的数据修改,您可以随时获取pyexcel表格,然后继续进行进一步的格式化,然后调用save_to_django_model。

        sheet = request.FILES['file'].get_sheet()
        ...process sheet data..
        sheet.save_to_django_model(
            model=Transaksi,
            mapdict=['tanggal', 'kode','unit','keterangan', 'nominal'],

        )
        return HttpResponse("OK", status=200)

这是示例数据整理代码:

>>> import pyexcel as p
>>> s=p.get_sheet(file_name='42381732.tsv')
>>> s
42381732.tsv:
+------------+----------------------------------------+---------+
| tangal     | keterangan                             | nominal |
+------------+----------------------------------------+---------+
| 19/01/2016 | 944448800010303282-SPP Raihanah Barmim | 205000  |
+------------+----------------------------------------+---------+
| 19/02/2016 | 944448800010303282-SPP Raihanah Barmim | 205000  |
+------------+----------------------------------------+---------+
>>> rows = [row.split() for row in s.column[1][1:]]
>>> rows
[[u'944448800010303282-SPP', u'Raihanah', u'Barmim'], [u'944448800010303282-SPP', u'Raihanah', u'Barmim']]
>>> rows= [[row[0], ' '.join(row[1:])] for row in rows]
>>> rows
[[u'944448800010303282-SPP', u'Raihanah Barmim'], [u'944448800010303282-SPP', u'Raihanah Barmim']]
>>> rows = [['kode', 'unit']] + rows
>>> rows
[['kode', 'unit'], [u'944448800010303282-SPP', u'Raihanah Barmim'], [u'944448800010303282-SPP', u'Raihanah Barmim']]
>>> s.extend_columns_with_rows(rows)
>>> s
42381732.tsv.tsv:
+------------+----------------------------------------+---------+------------------------+-----------------+
| tangal     | keterangan                             | nominal | kode                   | unit            |
+------------+----------------------------------------+---------+------------------------+-----------------+
| 19/01/2016 | 944448800010303282-SPP Raihanah Barmim | 205000  | 944448800010303282-SPP | Raihanah Barmim |
+------------+----------------------------------------+---------+------------------------+-----------------+
| 19/02/2016 | 944448800010303282-SPP Raihanah Barmim | 205000  | 944448800010303282-SPP | Raihanah Barmim |
+------------+----------------------------------------+---------+------------------------+-----------------+
于 2017-03-01T16:22:27.467 回答