def insert(self, model_instances, batch_size=<NUM_LIT:1000>):
from six import next<EOL>from io import BytesIO<EOL>i = iter(model_instances)<EOL>try:<EOL><INDENT>first_instance = next(i)<EOL><DEDENT>except StopIteration:<EOL><INDENT>return  <EOL><DEDENT>model_class = first_instance.__class__<EOL>if first_instance.is_read_only() or first_instance.is_system_model():<EOL><INDENT>raise DatabaseException("<STR_LIT>")<EOL><DEDENT>fields_list = '<STR_LIT:U+002C>'.join(<EOL>['<STR_LIT>' % name for name in first_instance.fields(writable=True)])<EOL>def gen():<EOL><INDENT>buf = BytesIO()<EOL>query = '<STR_LIT>' % fields_list<EOL>buf.write(self._substitute(query, model_class).encode('<STR_LIT:utf-8>'))<EOL>first_instance.set_database(self)<EOL>buf.write(first_instance.to_tsv(include_readonly=False).encode('<STR_LIT:utf-8>'))<EOL>buf.write('<STR_LIT:\n>'.encode('<STR_LIT:utf-8>'))<EOL>lines = <NUM_LIT:2><EOL>for instance in i:<EOL><INDENT>instance.set_database(self)<EOL>buf.write(instance.to_tsv(include_readonly=False).encode('<STR_LIT:utf-8>'))<EOL>buf.write('<STR_LIT:\n>'.encode('<STR_LIT:utf-8>'))<EOL>lines += <NUM_LIT:1><EOL>if lines >= batch_size:<EOL><INDENT>yield buf.getvalue()<EOL>buf = BytesIO()<EOL>lines = <NUM_LIT:0><EOL><DEDENT><DEDENT>if lines:<EOL><INDENT>yield buf.getvalue()<EOL><DEDENT><DEDENT>self._send(gen())<EOL>
Insert records into the database.

- `model_instances`: any iterable containing instances of a single model class.
- `batch_size`: number of records to send per chunk (use a lower number if your records are very large).