Repository: wagtail/wagtail

Issue:
manage.py compress errors with empty db
In 1.1rc1 manage.py command 'compress' doesn't work anymore if the database isn't migrated yet. I need to be able to do this since i'm running django compress during a docker build.

Complete stacktrace is:

```
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/debug_toolbar/apps.py", line 15, in ready
    dt_settings.patch_all()
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/debug_toolbar/settings.py", line 232, in patch_all
    patch_root_urlconf()
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/debug_toolbar/settings.py", line 220, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/django/core/urlresolvers.py", line 550, in reverse
    app_list = resolver.app_dict[ns]
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/django/core/urlresolvers.py", line 352, in app_dict
    self._populate()
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/django/core/urlresolvers.py", line 285, in _populate
    for pattern in reversed(self.url_patterns):
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/django/core/urlresolvers.py", line 402, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/django/core/urlresolvers.py", line 396, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/mvantellingen/projects/myorg/myproj/src/myproj/urls.py", line 7, in <module>
    from wagtail.wagtailadmin import urls as wagtailadmin_urls
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/wagtail/wagtailadmin/urls/__init__.py", line 7, in <module>
    from wagtail.wagtailadmin.views import account, chooser, home, pages, tags, userbar
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/wagtail/wagtailadmin/views/account.py", line 12, in <module>
    from wagtail.wagtailusers.forms import NotificationPreferencesForm
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/wagtail/wagtailusers/forms.py", line 236, in <module>
    class GroupPagePermissionForm(forms.ModelForm):
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/wagtail/wagtailusers/forms.py", line 238, in GroupPagePermissionForm
    widget=AdminPageChooser(show_edit_link=False))
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/wagtail/wagtailadmin/widgets.py", line 123, in __init__
    self.target_content_types = content_type or ContentType.objects.get_for_model(Page)
  File "/usr/local/opt/pyenv/versions/myproj/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 78, in get_for_model
    "Error creating new content types. Please make sure contenttypes "
RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.
```



Discussion:
Thanks for reporting! We should move https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailadmin/widgets.py#L123 into its own method I think. So it only runs when the form is being rendered.


Resolving patch:
diff --git a/wagtail/wagtailadmin/widgets.py b/wagtail/wagtailadmin/widgets.py
--- a/wagtail/wagtailadmin/widgets.py
+++ b/wagtail/wagtailadmin/widgets.py
@@ -5,6 +5,7 @@
 from django.core.urlresolvers import reverse
 from django.forms import widgets
 from django.contrib.contenttypes.models import ContentType
+from django.utils.functional import cached_property
 from django.utils.translation import ugettext_lazy as _
 from django.template.loader import render_to_string
 
@@ -119,11 +120,15 @@ class AdminPageChooser(AdminChooser):
 
     def __init__(self, content_type=None, **kwargs):
         super(AdminPageChooser, self).__init__(**kwargs)
+        self._content_type = content_type
 
-        self.target_content_types = content_type or ContentType.objects.get_for_model(Page)
+    @cached_property
+    def target_content_types(self):
+        target_content_types = self._content_type or ContentType.objects.get_for_model(Page)
         # Make sure target_content_types is a list or tuple
-        if not isinstance(self.target_content_types, (list, tuple)):
-            self.target_content_types = [self.target_content_types]
+        if not isinstance(target_content_types, (list, tuple)):
+            target_content_types = [target_content_types]
+        return target_content_types
 
     def render_html(self, name, value, attrs):
         if len(self.target_content_types) == 1:


Test patch: