def todjango(table, model, update=True, create=True, use_bulk_create=True, *args, **kwargs):
assert issubclass(model, Model), '<STR_LIT>'<EOL>table_iterator = iter(table)<EOL>table_headers = table_iterator.next()<EOL>model_pk_field_name = model._meta.pk.name<EOL>model_field_names = _get_model_field_names(model)<EOL>model_name = model.__name__<EOL>existing_models = _get_django_objects(model)<EOL>existing_model_map = dict([(m.pk, m) for m in existing_models])<EOL>if update:<EOL><INDENT>assert model_pk_field_name in set(model_field_names), '<STR_LIT>'.format(model_pk_field_name)<EOL><DEDENT>updated_model_count = <NUM_LIT:0><EOL>unsaved_models = []<EOL>for row in table_iterator:<EOL><INDENT>value_map = dict(zip(table_headers, row))<EOL>pk = value_map.get(model_pk_field_name, None)<EOL>try:<EOL><INDENT>django_object = existing_model_map[pk]<EOL>if _will_model_change(value_map, django_object):<EOL><INDENT>_apply_value_map(value_map, django_object)<EOL>try:<EOL><INDENT>django_object.save()<EOL><DEDENT>except Exception as e:<EOL><INDENT>e.petl_data = value_map<EOL>raise e<EOL><DEDENT>updated_model_count += <NUM_LIT:1><EOL><DEDENT><DEDENT>except KeyError:<EOL><INDENT>django_object = model(**value_map)<EOL>if use_bulk_create:<EOL><INDENT>unsaved_models.append(django_object)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>django_object.save()<EOL><DEDENT>except Exception as e:<EOL><INDENT>e.petl_data = value_map<EOL>raise e<EOL><DEDENT><DEDENT><DEDENT><DEDENT>logger.debug('<STR_LIT>'.format(model_name))<EOL>if use_bulk_create:<EOL><INDENT>_chunked_bulk_create(model, unsaved_models)<EOL><DEDENT>msg = '<STR_LIT>'.format(updated_model_count, model_name)<EOL>logger.info(msg)<EOL>msg = '<STR_LIT>'.format(len(unsaved_models), model_name)<EOL>logger.info(msg)<EOL>
Given a table with appropriate headings create Django models.