如何在django中将Excel中的文本从excel分解为2个模型?

时间:2017-02-22 02:41:50

标签: python django pyexcel django-excel

当我在ms excel中从一列中爆炸文本并将其插入Django模型中的2列时,我遇到了问题。 这是我的views.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'和'单位'。怎么做?

1 个答案:

答案 0 :(得分:1)

save_to_database是一种将Excel工作表导入数据库模型的便捷方法。如果电子表格需要进一步的数据调整,您始终可以获得pyexcel sheet然后继续进行进一步格式化,然后调用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 |
+------------+----------------------------------------+---------+------------------------+-----------------+