# Buggy code
def handle(self, *args, **options):
    api = slumber.API(base_url="http://readthedocs.org/api/v1/")
    user1 = User.objects.filter(pk__gt=0).order_by("pk").first()

    for slug in options["project_slug"]:
        self.stdout.write("Importing {slug} ...".format(slug=slug))

        project_data = api.project.get(slug=slug)
        try:
            project_data = project_data["objects"][0]
        except (KeyError, IndexError):
            self.stderr.write(
                "Cannot find {slug} in API. Response was:\n{response}".format(
                    slug=slug, response=json.dumps(project_data)
                )
            )

        try:
            project = Project.objects.get(slug=slug)
        except Project.DoesNotExist:
            project = Project(slug=slug)

        exclude_attributes = (
            "absolute_url",
            "analytics_code",
            "canonical_url",
            "users",
        )

        for attribute in project_data:
            if attribute not in exclude_attributes:
                setattr(project, attribute, project_data[attribute])
        project.user = user1
        project.save()
        if user1:
            project.users.add(user1)

        call_command("update_repos", project.slug, record=True, version="all")


# Traceback (HttpClientError)
$ python manage.py update_repos pip
[28/Feb/2018 04:43:25] root:124[23847]: INFO Generating grammar tables from /usr/lib/python2.7/lib2to3/Grammar.txt
[28/Feb/2018 04:43:25] root:124[23847]: INFO Generating grammar tables from /usr/lib/python2.7/lib2to3/PatternGrammar.txt
System check identified some issues:

WARNINGS:
?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DEBUG.
?: (guardian.W001) Guardian authentication backend is not hooked. You can add this in settings as eg: `AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend', 'guardian.backends.ObjectPermissionBackend')`.
[28/Feb/2018 04:43:27] readthedocs.core.management.commands.update_repos:80[23847]: INFO Building Pip
[28/Feb/2018 04:43:27] requests.packages.urllib3.connectionpool:207[23847]: INFO Starting new HTTP connection (1): localhost
[28/Feb/2018 04:43:27] requests.packages.urllib3.connectionpool:242[23847]: INFO Resetting dropped connection: localhost
[28/Feb/2018 04:43:27] readthedocs.projects.tasks:233[23847]: INFO (Build) [pip:latest] Updating docs from VCS
[28/Feb/2018 04:43:27] readthedocs.projects.tasks:233[23847]: INFO (Build) [pip:latest] Checking out version latest: master
[28/Feb/2018 04:43:27] readthedocs.doc_builder.environments:117[23847]: INFO Running: 'git status' [/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/user_builds/pip/checkouts/latest]
[28/Feb/2018 04:43:27] readthedocs.doc_builder.environments:117[23847]: INFO Running: 'git remote set-url origin https://github.com/pypa/pip' [/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/user_builds/pip/checkouts/latest]
[28/Feb/2018 04:43:27] readthedocs.doc_builder.environments:117[23847]: INFO Running: 'git fetch --tags --prune' [/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/user_builds/pip/checkouts/latest]
[28/Feb/2018 04:43:29] readthedocs.doc_builder.environments:117[23847]: INFO Running: 'git show-ref remotes/origin/master' [/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/user_builds/pip/checkouts/latest]
[28/Feb/2018 04:43:29] readthedocs.doc_builder.environments:193[23847]: WARNING Recording command exit_code as success
[28/Feb/2018 04:43:29] requests.packages.urllib3.connectionpool:242[23847]: INFO Resetting dropped connection: localhost
[28/Feb/2018 04:43:29] readthedocs.vcs_support.utils:101[23847]: INFO Lock (pip): Releasing
[28/Feb/2018 04:43:29] readthedocs.doc_builder.environments:450[23847]: ERROR (Build) [pip:latest] Client Error 400: http://localhost:8000/api/v2/command/
Traceback (most recent call last):
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 333, in run_setup
self.setup_vcs()
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 452, in setup_vcs
self.sync_repo()
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 124, in sync_repo
version_repo.checkout(self.version.identifier)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/vcs_support/backends/git.py", line 183, in checkout
identifier = self.find_ref(identifier)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/vcs_support/backends/git.py", line 207, in find_ref
if self.ref_exists('remotes/origin/' + ref):
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/vcs_support/backends/git.py", line 213, in ref_exists
code, _, _ = self.run('git', 'show-ref', ref, record_as_success=True)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/vcs_support/base.py", line 97, in run
build_cmd = self.environment.run(*cmd, **kwargs)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 470, in run
return super(BuildEnvironment, self).run(*cmd, **kwargs)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 310, in run
return self.run_command_class(cls=self.command_class, cmd=cmd, **kwargs)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 476, in run_command_class
return super(BuildEnvironment, self).run_command_class(*cmd, **kwargs)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 356, in run_command_class
self.record_command(build_cmd)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 456, in record_command
command.save()
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/doc_builder/environments.py", line 205, in save
api_v2.command.post(data)
File "/home/rm/src/readthedocs/rtd/local/lib/python2.7/site-packages/slumber/__init__.py", line 167, in post
resp = self._request("POST", data=data, files=files, params=kwargs)
File "/home/rm/src/readthedocs/rtd/local/lib/python2.7/site-packages/slumber/__init__.py", line 101, in _request
raise exception_class("Client Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content)
HttpClientError: Client Error 400: http://localhost:8000/api/v2/command/
[28/Feb/2018 04:43:29] readthedocs.doc_builder.environments:431[23847]: INFO (Build) [pip:latest] Build finished
[28/Feb/2018 04:43:29] readthedocs.projects.tasks:233[23847]: INFO (Build) [pip:latest] Failing build because of setup failure: Client Error 400: http://localhost:8000/api/v2/command/
[28/Feb/2018 04:43:29] readthedocs.projects.tasks:284[23847]: ERROR An unhandled exception was raised during build setup
Traceback (most recent call last):
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 276, in run
setup_successful = self.run_setup(record=record)
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 355, in run_setup
self.send_notifications()
File "/home/rm/src/readthedocs/rtd/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 663, in send_notifications
send_notifications.delay(self.version.pk, build_pk=self.build['id'])
KeyError: 'id'
[28/Feb/2018 04:43:29] celery.app.trace:123[23847]: INFO Task readthedocs.projects.tasks.update_docs[87cb2b57-91fb-48b7-b97b-ec22c31099f1] succeeded in 1.796398073s: False

# Fixed code
def handle(self, *args, **options):
    api = slumber.API(base_url="http://readthedocs.org/api/v1/")
    user1 = User.objects.filter(pk__gt=0).order_by("pk").first()

    for slug in options["project_slug"]:
        self.stdout.write("Importing {slug} ...".format(slug=slug))

        project_data = api.project.get(slug=slug)
        try:
            project_data = project_data["objects"][0]
        except (KeyError, IndexError):
            self.stderr.write(
                "Cannot find {slug} in API. Response was:\n{response}".format(
                    slug=slug, response=json.dumps(project_data)
                )
            )

        try:
            project = Project.objects.get(slug=slug)
        except Project.DoesNotExist:
            project = Project(slug=slug)

        exclude_attributes = (
            "absolute_url",
            "analytics_code",
            "canonical_url",
            "users",
        )

        for attribute in project_data:
            if attribute not in exclude_attributes:
                setattr(project, attribute, project_data[attribute])
        project.user = user1
        project.save()
        if user1:
            project.users.add(user1)

        call_command("update_repos", project.slug, version="all")