如何在django中将excel中的文本从excel扩展为2个模型? [英] How to explode text in one column from excel into 2 model in django?

查看:219
本文介绍了如何在django中将excel中的文本从excel扩展为2个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在ms excel中从一列中打开文本时,我有一个问题,并将其插入Django模型中的2列。
这是我的views.py

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

def import_data(request):
如果request.method ==POST:
form = UploadFileForm(request.POST,request.FILES)
如果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

  Transaksi(models.Model):
tanggal = models.DateField(null = True,blank = True)
kode = m odels.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'和'单位'。如何做?

解决方案

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

  sheet = request.FILES ['文件']。get_sheet()
...进程表数据
sheet.save_to_django_model(
model = Transaksi,
mapdict = ['tanggal','kode' 'unit','keterangan','nominal'],


return HttpResponse(OK,status = 200)
pre>

以下是示例数据争吵代码:

 > >>将pyexcel导入为p 
>>> s = p.get_sheet(file_name ='42381732.tsv')
>>> s
42381732.tsv:
+ ------------ + ----------------------- ----------------- + --------- +
| tangal | keterangan |名义|
+ ------------ + -------------------------------- -------- + --------- +
| 19/01/2016 | 944448800010303282-SPP Raihanah Barmim | 205000 |
+ ------------ + -------------------------------- -------- + --------- +
| 19/02/2016 | 944448800010303282-SPP Raihanah Barmim | 205000 |
+ ------------ + -------------------------------- -------- + --------- +
>>> rows = [row.split()for s.column [1] [1:]]
>>>>行
[[u'944448800010303282-SPP',u'Raihanah',u'Barmim'],[u'944448800010303282-SPP',u'Raihanah',u'Barmim']]
> >> rows = [[row [0],''.join(row [1:])] for row in rows]
>>>行
[[u'944448800010303282-SPP',u'Raihanah Barmim'],[u'944448800010303282-SPP',u'Raihanah Barmim']]
>>> rows = [['kode','unit']] + 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 |名义| kode |单位|
+ ------------ + -------------------------------- -------- + --------- + ------------------------ + ------ ----------- +
| 19/01/2016 | 944448800010303282-SPP Raihanah Barmim | 205000 | 944448800010303282-SPP | Raihanah Barmim |
+ ------------ + -------------------------------- -------- + --------- + ------------------------ + ------ ----------- +
| 19/02/2016 | 944448800010303282-SPP Raihanah Barmim | 205000 | 944448800010303282-SPP | Raihanah Barmim |
+ ------------ + -------------------------------- -------- + --------- + ------------------------ + ------ ----------- +


I have a problem when exploding text from one column in ms excel and insert it into 2 column in Django models. this is my 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})

it's my 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)

and my excel file consist just three columns

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

I want to split text from 'keterangan' column into my Django model field there are 'kode' and 'unit'. How to do it?

解决方案

save_to_database is a convenient method to import an excel sheet into a database model only. If the spread sheet needs further data munging, you can always obtain pyexcel sheet and then continue to do further formatting, then call 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)

Here is the example data wrangling code:

>>> 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 |
+------------+----------------------------------------+---------+------------------------+-----------------+

这篇关于如何在django中将excel中的文本从excel扩展为2个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆