如何在django中插入多条记录?

时间:2018-05-03 04:42:38

标签: python django

views.py

def input_transaksi(request):    
    kode_pem = dict(request.POST)['kode_pem']
    nominal = dict(request.POST)['nominal']
    rincian = Transaksi(kode_pem=kode_pem,nominal=nominal)
    rincian.save()

model.py

class Transaksi(models.Model):
    nominal = models.IntegerField(null=True, blank=True)
    kode_pem = models.CharField(max_length=2, null=True, blank=True)

然后我输入此表单中的数据

{% for t in trans %} #for looping form
    <from  method="post" action="{% url 'input_transaksi' %}" enctype="multipart/form-data">    
    <input type="hidden" name="kode_pem">           
        <input type="hidden" name="nominal">
    <input type="submit" value="Insert" class="btn btn-primary">
    </form>
{% endfor %}

但是我收到了这个错误:

Environment: Request Method: POST Request URL: http://localhost:8000/transaksi/input_transaksi

Django Version: 1.10.4 Python Version: 2.7.11 Installed Applications: ['django.contrib.admin',  'django.contrib.auth',  'django.contrib.contenttypes',  'django.contrib.sessions',  'django.contrib.messages',  'django.contrib.staticfiles',  'django.contrib.humanize',  'datapribadisiswa',  'riwayatsekolah',  'riwayatprestasi',  'dataguru',  'biaya_pendidikan',  'transaksi_kas', 'kodepembayaran',  'django_ajax',  'import_export'] Installed Middleware: ['django.middleware.security.SecurityMiddleware',  'django.contrib.sessions.middleware.SessionMiddleware',  'django.middleware.common.CommonMiddleware',  'django.middleware.csrf.CsrfViewMiddleware',  'django.contrib.auth.middleware.AuthenticationMiddleware',  'django.contrib.messages.middleware.MessageMiddleware',  'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\core\handlers\base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\Lenovo\OneDrive\siswa\transaksi_kas\views.py" in input_transaksi
  1213.     rincian.save()

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\base.py" in save
  796.                        force_update=force_update, update_fields=update_fields)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\base.py" in save_base
  824.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\base.py" in _save_table
  908.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\base.py" in _do_insert
  947.                                using=using, raw=raw)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\manager.py" in manager_method
  85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\query.py" in _insert
  1045.         return query.get_compiler(using=using).execute_sql(return_id)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\sql\compiler.py" in execute_sql
  1053.             for sql, params in self.as_sql():

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\sql\compiler.py" in as_sql
  1006.                 for obj in self.query.objs

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\sql\compiler.py" in prepare_value
  945.             value = field.get_db_prep_save(value, connection=self.connection)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\fields\__init__.py" in get_db_prep_save
  755.                                       prepared=False)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\fields\__init__.py" in get_db_prep_value
  747.             value = self.get_prep_value(value)

File "C:\Python27\lib\site-packages\django-1.10.4-py2.7.egg\django\db\models\fields\__init__.py" in get_prep_value
  1832.         return int(value)

Exception Type: TypeError at /transaksi/input_transaksi Exception Value: int() argument must be a string or a number, not 'list'

我该如何解决?

1 个答案:

答案 0 :(得分:1)

您可以使用批量创建方法。

此方法以有效的方式将提供的对象列表插入到数据库中(通常只有1个查询,无论有多少个对象):

请参阅文档bulk-create