# Buggy code
def handle(self, *args, **options):
    engine = create_engine(get_default_db_string(), convert_unicode=True)

    metadata = MetaData()

    app_config = apps.get_app_config("content")
    # Exclude channelmetadatacache in case we are reflecting an older version of Kolibri
    table_names = [
        model._meta.db_table
        for name, model in app_config.models.items()
        if name != "channelmetadatacache"
    ]
    metadata.reflect(bind=engine, only=table_names)
    Base = automap_base(metadata=metadata)
    # TODO map relationship backreferences using the django names
    Base.prepare()
    session = sessionmaker(bind=engine, autoflush=False)()

    # Load fixture data into the test database with Django
    call_command("loaddata", "content_import_test.json", interactive=False)

    def get_dict(item):
        value = {
            key: value
            for key, value in item.__dict__.items()
            if key != "_sa_instance_state"
        }
        return value

    data = {}

    for table_name, record in Base.classes.items():
        data[table_name] = [get_dict(r) for r in session.query(record).all()]

    with open(SCHEMA_PATH_TEMPLATE.format(name=options["version"]), "wb") as f:
        pickle.dump(metadata, f, protocol=2)

    with open(DATA_PATH_TEMPLATE.format(name=options["version"]), "wb") as f:
        json.dump(data, f)


# Traceback (TypeError)
didi@didi-VirtualBox:~/Desktop$ ./kolibri-0.6.dev020171007224512-git.pex manage importchannel -- network fb51dae6df7545af8455aa3a0c32048d
INFO     Version was 0.6.dev020171009005906-export, new version: 0.6.dev020171009010641-export
INFO     Running update routines for new version...

0 static files copied to '/home/didi/.kolibri/static', 405 unmodified.
Operations to perform:
Apply all migrations: sessions, admin, kolibriauth, morango, auth, content, contenttypes, exams, device, logger
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
INFO     Downloading data for channel id fb51dae6df7545af8455aa3a0c32048d
100%|███████████████████████████████████████████████████████████████████████████████| 421888/421888 [00:00<00:00, 902015.67it/s]INFO     Importing ContentTag data
INFO:kolibri.content.utils.channel_import:Importing ContentTag data
INFO     Importing ContentNode_has_prerequisite data
INFO:kolibri.content.utils.channel_import:Importing ContentNode_has_prerequisite data
INFO     Importing ContentNode_related data
INFO:kolibri.content.utils.channel_import:Importing ContentNode_related data
INFO     Importing ContentNode_tags data
INFO:kolibri.content.utils.channel_import:Importing ContentNode_tags data
INFO     Importing ContentNode data
INFO:kolibri.content.utils.channel_import:Importing ContentNode data

Traceback (most recent call last):
File "/home/didi/Desktop/kolibri-0.6.dev020171007224512-git.pex/.bootstrap/_pex/pex.py", line 361, in execute
File "/home/didi/Desktop/kolibri-0.6.dev020171007224512-git.pex/.bootstrap/_pex/pex.py", line 289, in _wrap_coverage
File "/home/didi/Desktop/kolibri-0.6.dev020171007224512-git.pex/.bootstrap/_pex/pex.py", line 321, in _wrap_profiling
File "/home/didi/Desktop/kolibri-0.6.dev020171007224512-git.pex/.bootstrap/_pex/pex.py", line 404, in _execute
File "/home/didi/Desktop/kolibri-0.6.dev020171007224512-git.pex/.bootstrap/_pex/pex.py", line 462, in execute_entry
File "/home/didi/Desktop/kolibri-0.6.dev020171007224512-git.pex/.bootstrap/_pex/pex.py", line 467, in execute_module
File "/usr/lib/python2.7/runpy.py", line 192, in run_module
fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/__main__.py", line 8, in <module>
main(args=sys.argv[1:])
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/utils/cli.py", line 616, in main
manage(command, args=django_args)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/utils/cli.py", line 447, in manage
execute_from_command_line(argv=argv)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/dist/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/dist/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/dist/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/dist/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/tasks/management/commands/base.py", line 99, in handle
return self.handle_async(*args, **options)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/content/management/commands/importchannel.py", line 89, in handle_async
self.download_channel(options["channel_id"])
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/content/management/commands/importchannel.py", line 42, in download_channel
self._transfer(DOWNLOAD_METHOD, channel_id)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/content/management/commands/importchannel.py", line 79, in _transfer
channel_import.import_channel_from_local_db(channel_id)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/content/utils/channel_import.py", line 326, in import_channel_from_local_db
import_manager.import_channel_data()
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/content/utils/channel_import.py", line 197, in import_channel_data
unflushed_rows = self.table_import(model, row_mapper, table_mapper, unflushed_rows)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/content/utils/channel_import.py", line 167, in table_import
str(column): row_mapper(record, column) for column in columns if row_mapper(record, column) is not None
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/content/utils/channel_import.py", line 167, in <dictcomp>
str(column): row_mapper(record, column) for column in columns if row_mapper(record, column) is not None
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/content/utils/channel_import.py", line 123, in mapper
return getattr(self, col_map)(record)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/content/utils/channel_import.py", line 297, in get_license_description
license = self.get_license(SourceRecord)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/content/utils/channel_import.py", line 288, in get_license
license = self.source.session.query(LicenseRecord).get(license_id)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/dist/sqlalchemy/orm/query.py", line 864, in get
return self._get_impl(ident, loading.load_on_ident)
File "/home/didi/.pex/install/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl.937a9da87197a4a2349e3f6bdc8ebe4334b02233/kolibri-0.6.dev020171007224512_git-py2.py3-none-any.whl/kolibri/dist/sqlalchemy/orm/query.py", line 875, in _get_impl
if len(ident) != len(mapper.primary_key):
TypeError: object of type 'NoneType' has no len()
didi@didi-VirtualBox:~/Desktop$

# Fixed code
def handle(self, *args, **options):
    engine = create_engine(get_default_db_string(), convert_unicode=True)

    metadata = MetaData()

    app_config = apps.get_app_config("content")
    # Exclude channelmetadatacache in case we are reflecting an older version of Kolibri
    table_names = [
        model._meta.db_table
        for name, model in app_config.models.items()
        if name != "channelmetadatacache"
    ]
    metadata.reflect(bind=engine, only=table_names)
    Base = automap_base(metadata=metadata)
    # TODO map relationship backreferences using the django names
    Base.prepare()
    session = sessionmaker(bind=engine, autoflush=False)()

    # Load fixture data into the test database with Django
    call_command("loaddata", "content_import_test.json", interactive=False)

    def get_dict(item):
        value = {
            key: value
            for key, value in item.__dict__.items()
            if key != "_sa_instance_state"
        }
        return value

    data = {}

    for table_name, record in Base.classes.items():
        data[table_name] = [get_dict(r) for r in session.query(record).all()]

    with open(SCHEMA_PATH_TEMPLATE.format(name=options["version"]), "wb") as f:
        pickle.dump(metadata, f, protocol=2)

    with open(DATA_PATH_TEMPLATE.format(name=options["version"]), "w") as f:
        json.dump(data, f)