Skip to content

Commit

Permalink
Fixed #24146 -- Allowed model._meta.get_field() to be used after apps…
Browse files Browse the repository at this point in the history
….models_ready
  • Loading branch information
PirosB3 authored and timgraham committed Feb 11, 2015
1 parent 1fbe8a2 commit 1918882
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion django/db/models/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def get_field(self, field_name, many_to_many=None):
except KeyError:
# If the app registry is not ready, reverse fields are
# unavailable, therefore we throw a FieldDoesNotExist exception.
if not self.apps.ready:
if not self.apps.models_ready:
raise FieldDoesNotExist(
"%s has no field named %r. The app cache isn't ready yet, "
"so if this is an auto-created related field, it won't "
Expand Down
1 change: 1 addition & 0 deletions tests/admin_checks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Book(models.Model):
class AuthorsBooks(models.Model):
author = models.ForeignKey(Author)
book = models.ForeignKey(Book)
featured = models.BooleanField()


class State(models.Model):
Expand Down
17 changes: 17 additions & 0 deletions tests/admin_checks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,20 @@ class MyModelAdmin(admin.ModelAdmin):
)
]
self.assertEqual(errors, expected)

def test_list_filter_works_on_through_field_even_when_apps_not_ready(self):
"""
Ensure list_filter can access reverse fields even when the app registry
is not ready; refs #24146.
"""
class BookAdminWithListFilter(admin.ModelAdmin):
list_filter = ['authorsbooks__featured']

# Temporarily pretending apps are not ready yet. This issue can happen
# if the value of 'list_filter' refers to a 'through__field'.
Book._meta.apps.ready = False
try:
errors = BookAdminWithListFilter.check(model=Book)
self.assertEqual(errors, [])
finally:
Book._meta.apps.ready = True
4 changes: 2 additions & 2 deletions tests/model_meta/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_get_fields_only_searches_forward_on_apps_not_ready(self):
opts = Person._meta
# If apps registry is not ready, get_field() searches over only
# forward fields.
opts.apps.ready = False
opts.apps.models_ready = False
try:
# 'data_abstract' is a forward field, and therefore will be found
self.assertTrue(opts.get_field('data_abstract'))
Expand All @@ -191,7 +191,7 @@ def test_get_fields_only_searches_forward_on_apps_not_ready(self):
with self.assertRaisesMessage(FieldDoesNotExist, msg):
opts.get_field('relating_baseperson')
finally:
opts.apps.ready = True
opts.apps.models_ready = True


class RelationTreeTests(TestCase):
Expand Down

0 comments on commit 1918882

Please sign in to comment.