mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #24075 -- Used post-migration models in contrib apps receivers.
Thanks Markus and Tim for the review.
This commit is contained in:
@@ -6,7 +6,7 @@ from __future__ import unicode_literals
|
||||
import getpass
|
||||
import unicodedata
|
||||
|
||||
from django.apps import apps
|
||||
from django.apps import apps as global_apps
|
||||
from django.contrib.auth import get_permission_codename
|
||||
from django.core import exceptions
|
||||
from django.db import DEFAULT_DB_ALIAS, router
|
||||
@@ -37,11 +37,14 @@ def _get_builtin_permissions(opts):
|
||||
return perms
|
||||
|
||||
|
||||
def create_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs):
|
||||
def create_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, apps=global_apps, **kwargs):
|
||||
if not app_config.models_module:
|
||||
return
|
||||
|
||||
app_label = app_config.label
|
||||
try:
|
||||
app_config = apps.get_app_config(app_label)
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
Permission = apps.get_model('auth', 'Permission')
|
||||
except LookupError:
|
||||
return
|
||||
@@ -49,8 +52,6 @@ def create_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_
|
||||
if not router.allow_migrate_model(using, Permission):
|
||||
return
|
||||
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
# This will hold the permissions we're looking for as
|
||||
# (content_type, (codename, name))
|
||||
searched_perms = list()
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
from django.apps import apps
|
||||
from django.apps import apps as global_apps
|
||||
from django.db import DEFAULT_DB_ALIAS, router
|
||||
from django.utils import six
|
||||
from django.utils.six.moves import input
|
||||
|
||||
|
||||
def update_contenttypes(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs):
|
||||
def update_contenttypes(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, apps=global_apps, **kwargs):
|
||||
"""
|
||||
Creates content types for models in the given app, removing any model
|
||||
entries that no longer have a matching model class.
|
||||
@@ -12,7 +12,9 @@ def update_contenttypes(app_config, verbosity=2, interactive=True, using=DEFAULT
|
||||
if not app_config.models_module:
|
||||
return
|
||||
|
||||
app_label = app_config.label
|
||||
try:
|
||||
app_config = apps.get_app_config(app_label)
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
except LookupError:
|
||||
return
|
||||
@@ -21,8 +23,9 @@ def update_contenttypes(app_config, verbosity=2, interactive=True, using=DEFAULT
|
||||
return
|
||||
|
||||
ContentType.objects.clear_cache()
|
||||
|
||||
app_label = app_config.label
|
||||
# Always clear the global content types cache.
|
||||
if apps is not global_apps:
|
||||
global_apps.get_model('contenttypes', 'ContentType').objects.clear_cache()
|
||||
|
||||
app_models = {
|
||||
model._meta.model_name: model
|
||||
|
||||
@@ -2,7 +2,6 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.apps import apps
|
||||
from django.db import models
|
||||
from django.db.utils import IntegrityError, OperationalError, ProgrammingError
|
||||
from django.utils.encoding import force_text, python_2_unicode_compatible
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
@@ -48,24 +47,15 @@ class ContentTypeManager(models.Manager):
|
||||
# The ContentType entry was not found in the cache, therefore we
|
||||
# proceed to load or create it.
|
||||
try:
|
||||
try:
|
||||
# We start with get() and not get_or_create() in order to use
|
||||
# the db_for_read (see #20401).
|
||||
ct = self.get(app_label=opts.app_label, model=opts.model_name)
|
||||
except self.model.DoesNotExist:
|
||||
# Not found in the database; we proceed to create it. This time we
|
||||
# use get_or_create to take care of any race conditions.
|
||||
ct, created = self.get_or_create(
|
||||
app_label=opts.app_label,
|
||||
model=opts.model_name,
|
||||
)
|
||||
except (OperationalError, ProgrammingError, IntegrityError):
|
||||
# It's possible to migrate a single app before contenttypes,
|
||||
# as it's not a required initial dependency (it's contrib!)
|
||||
# Have a nice error for this.
|
||||
raise RuntimeError(
|
||||
"Error creating new content types. Please make sure contenttypes "
|
||||
"is migrated before trying to migrate apps individually."
|
||||
# Start with get() and not get_or_create() in order to use
|
||||
# the db_for_read (see #20401).
|
||||
ct = self.get(app_label=opts.app_label, model=opts.model_name)
|
||||
except self.model.DoesNotExist:
|
||||
# Not found in the database; we proceed to create it. This time
|
||||
# use get_or_create to take care of any race conditions.
|
||||
ct, created = self.get_or_create(
|
||||
app_label=opts.app_label,
|
||||
model=opts.model_name,
|
||||
)
|
||||
self._add_to_cache(self.db, ct)
|
||||
return ct
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
Creates the default Site object.
|
||||
"""
|
||||
|
||||
from django.apps import apps
|
||||
from django.apps import apps as global_apps
|
||||
from django.conf import settings
|
||||
from django.core.management.color import no_style
|
||||
from django.db import DEFAULT_DB_ALIAS, connections, router
|
||||
|
||||
|
||||
def create_default_site(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs):
|
||||
def create_default_site(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, apps=global_apps, **kwargs):
|
||||
try:
|
||||
Site = apps.get_model('sites', 'Site')
|
||||
except LookupError:
|
||||
|
||||
Reference in New Issue
Block a user