We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
AppRegistryNotReady("Apps aren't loaded yet.")
### After updating wagtail from 2.1 to 2.2 I get an AppRegistryNotReady error when migrating.

Removing **from .sitemap_generator import Sitemap**
from **wagtail/contrib/sitemaps/__init__.py**
fixed the problem.

Maybe it has something to do with the User model; I use a custom one.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `wagtail/contrib/sitemaps/sitemap_generator.py`
Content:
```
1 import warnings
2 
3 from django.contrib.sitemaps import Sitemap as DjangoSitemap
4 
5 from wagtail.core.models import Site
6 from wagtail.core.utils import accepts_kwarg
7 from wagtail.utils.deprecation import RemovedInWagtail24Warning
8 
9 
10 class Sitemap(DjangoSitemap):
11 
12     def __init__(self, request=None):
13         self.request = request
14 
15     def location(self, obj):
16         return obj.get_full_url(self.request)
17 
18     def lastmod(self, obj):
19         # fall back on latest_revision_created_at if last_published_at is null
20         # (for backwards compatibility from before last_published_at was added)
21         return (obj.last_published_at or obj.latest_revision_created_at)
22 
23     def get_wagtail_site(self):
24         site = getattr(self.request, 'site', None)
25         if site is None:
26             return Site.objects.select_related(
27                 'root_page'
28             ).get(is_default_site=True)
29         return site
30 
31     def items(self):
32         return (
33             self.get_wagtail_site()
34             .root_page
35             .get_descendants(inclusive=True)
36             .live()
37             .public()
38             .order_by('path')
39             .specific())
40 
41     def _urls(self, page, protocol, domain):
42         urls = []
43         last_mods = set()
44 
45         for item in self.paginator.page(page).object_list:
46 
47             if not accepts_kwarg(item.get_sitemap_urls, 'request'):
48                 warnings.warn(
49                     "%s.get_sitemap_urls() must be updated to accept an optional "
50                     "'request' keyword argument" % type(item).__name__,
51                     category=RemovedInWagtail24Warning)
52 
53                 url_info_items = item.get_sitemap_urls()
54             else:
55                 url_info_items = item.get_sitemap_urls(self.request)
56 
57             for url_info in url_info_items:
58                 urls.append(url_info)
59                 last_mods.add(url_info.get('lastmod'))
60 
61         # last_mods might be empty if the whole site is private
62         if last_mods and None not in last_mods:
63             self.latest_lastmod = max(last_mods)
64         return urls
65
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/wagtail/contrib/sitemaps/sitemap_generator.py b/wagtail/contrib/sitemaps/sitemap_generator.py
--- a/wagtail/contrib/sitemaps/sitemap_generator.py
+++ b/wagtail/contrib/sitemaps/sitemap_generator.py
@@ -2,7 +2,6 @@
 
 from django.contrib.sitemaps import Sitemap as DjangoSitemap
 
-from wagtail.core.models import Site
 from wagtail.core.utils import accepts_kwarg
 from wagtail.utils.deprecation import RemovedInWagtail24Warning
 
@@ -23,6 +22,7 @@
     def get_wagtail_site(self):
         site = getattr(self.request, 'site', None)
         if site is None:
+            from wagtail.core.models import Site
             return Site.objects.select_related(
                 'root_page'
             ).get(is_default_site=True)

{"golden_diff": "diff --git a/wagtail/contrib/sitemaps/sitemap_generator.py b/wagtail/contrib/sitemaps/sitemap_generator.py\n--- a/wagtail/contrib/sitemaps/sitemap_generator.py\n+++ b/wagtail/contrib/sitemaps/sitemap_generator.py\n@@ -2,7 +2,6 @@\n \n from django.contrib.sitemaps import Sitemap as DjangoSitemap\n \n-from wagtail.core.models import Site\n from wagtail.core.utils import accepts_kwarg\n from wagtail.utils.deprecation import RemovedInWagtail24Warning\n \n@@ -23,6 +22,7 @@\n     def get_wagtail_site(self):\n         site = getattr(self.request, 'site', None)\n         if site is None:\n+            from wagtail.core.models import Site\n             return Site.objects.select_related(\n                 'root_page'\n             ).get(is_default_site=True)\n", "issue": "AppRegistryNotReady(\"Apps aren't loaded yet.\")\n### After updating wagtail from 2.1 to 2.2 I get an AppRegistryNotReady error when migrating.\r\n\r\nRemoving **from .sitemap_generator import Sitemap**\r\nfrom **wagtail/contrib/sitemaps/__init__.py**\r\nfixed the problem.\r\n\r\nMaybe it has something to do with the User model; I use a custom one.\n", "before_files": [{"content": "import warnings\n\nfrom django.contrib.sitemaps import Sitemap as DjangoSitemap\n\nfrom wagtail.core.models import Site\nfrom wagtail.core.utils import accepts_kwarg\nfrom wagtail.utils.deprecation import RemovedInWagtail24Warning\n\n\nclass Sitemap(DjangoSitemap):\n\n    def __init__(self, request=None):\n        self.request = request\n\n    def location(self, obj):\n        return obj.get_full_url(self.request)\n\n    def lastmod(self, obj):\n        # fall back on latest_revision_created_at if last_published_at is null\n        # (for backwards compatibility from before last_published_at was added)\n        return (obj.last_published_at or obj.latest_revision_created_at)\n\n    def get_wagtail_site(self):\n        site = getattr(self.request, 'site', None)\n        if site is None:\n            return Site.objects.select_related(\n                'root_page'\n            ).get(is_default_site=True)\n        return site\n\n    def items(self):\n        return (\n            self.get_wagtail_site()\n            .root_page\n            .get_descendants(inclusive=True)\n            .live()\n            .public()\n            .order_by('path')\n            .specific())\n\n    def _urls(self, page, protocol, domain):\n        urls = []\n        last_mods = set()\n\n        for item in self.paginator.page(page).object_list:\n\n            if not accepts_kwarg(item.get_sitemap_urls, 'request'):\n                warnings.warn(\n                    \"%s.get_sitemap_urls() must be updated to accept an optional \"\n                    \"'request' keyword argument\" % type(item).__name__,\n                    category=RemovedInWagtail24Warning)\n\n                url_info_items = item.get_sitemap_urls()\n            else:\n                url_info_items = item.get_sitemap_urls(self.request)\n\n            for url_info in url_info_items:\n                urls.append(url_info)\n                last_mods.add(url_info.get('lastmod'))\n\n        # last_mods might be empty if the whole site is private\n        if last_mods and None not in last_mods:\n            self.latest_lastmod = max(last_mods)\n        return urls\n", "path": "wagtail/contrib/sitemaps/sitemap_generator.py"}], "after_files": [{"content": "import warnings\n\nfrom django.contrib.sitemaps import Sitemap as DjangoSitemap\n\nfrom wagtail.core.utils import accepts_kwarg\nfrom wagtail.utils.deprecation import RemovedInWagtail24Warning\n\n\nclass Sitemap(DjangoSitemap):\n\n    def __init__(self, request=None):\n        self.request = request\n\n    def location(self, obj):\n        return obj.get_full_url(self.request)\n\n    def lastmod(self, obj):\n        # fall back on latest_revision_created_at if last_published_at is null\n        # (for backwards compatibility from before last_published_at was added)\n        return (obj.last_published_at or obj.latest_revision_created_at)\n\n    def get_wagtail_site(self):\n        site = getattr(self.request, 'site', None)\n        if site is None:\n            from wagtail.core.models import Site\n            return Site.objects.select_related(\n                'root_page'\n            ).get(is_default_site=True)\n        return site\n\n    def items(self):\n        return (\n            self.get_wagtail_site()\n            .root_page\n            .get_descendants(inclusive=True)\n            .live()\n            .public()\n            .order_by('path')\n            .specific())\n\n    def _urls(self, page, protocol, domain):\n        urls = []\n        last_mods = set()\n\n        for item in self.paginator.page(page).object_list:\n\n            if not accepts_kwarg(item.get_sitemap_urls, 'request'):\n                warnings.warn(\n                    \"%s.get_sitemap_urls() must be updated to accept an optional \"\n                    \"'request' keyword argument\" % type(item).__name__,\n                    category=RemovedInWagtail24Warning)\n\n                url_info_items = item.get_sitemap_urls()\n            else:\n                url_info_items = item.get_sitemap_urls(self.request)\n\n            for url_info in url_info_items:\n                urls.append(url_info)\n                last_mods.add(url_info.get('lastmod'))\n\n        # last_mods might be empty if the whole site is private\n        if last_mods and None not in last_mods:\n            self.latest_lastmod = max(last_mods)\n        return urls\n", "path": "wagtail/contrib/sitemaps/sitemap_generator.py"}]}