mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #2445 -- Allowed limit_choices_to attribute to be a callable.
ForeignKey or ManyToManyField attribute ``limit_choices_to`` can now be a callable that returns either a ``Q`` object or a dict. Thanks michael at actrix.gen.nz for the original suggestion.
This commit is contained in:
committed by
Tim Graham
parent
a718fcf201
commit
eefc88feef
@@ -52,7 +52,7 @@ from .models import (Article, BarAccount, CustomArticle, EmptyModel, FooAccount,
|
||||
Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
|
||||
Simple, UndeletableObject, UnchangeableObject, Choice, ShortMessage,
|
||||
Telegram, Pizza, Topping, FilteredManager, City, Restaurant, Worker,
|
||||
ParentWithDependentChildren)
|
||||
ParentWithDependentChildren, Character)
|
||||
from .admin import site, site2, CityAdmin
|
||||
|
||||
|
||||
@@ -3661,6 +3661,33 @@ class ReadonlyTest(TestCase):
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
||||
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
|
||||
class LimitChoicesToInAdminTest(TestCase):
|
||||
urls = "admin_views.urls"
|
||||
fixtures = ['admin-views-users.xml']
|
||||
|
||||
def setUp(self):
|
||||
self.client.login(username='super', password='secret')
|
||||
|
||||
def tearDown(self):
|
||||
self.client.logout()
|
||||
|
||||
def test_limit_choices_to_as_callable(self):
|
||||
"""Test for ticket 2445 changes to admin."""
|
||||
threepwood = Character.objects.create(
|
||||
username='threepwood',
|
||||
last_action=datetime.datetime.today() + datetime.timedelta(days=1),
|
||||
)
|
||||
marley = Character.objects.create(
|
||||
username='marley',
|
||||
last_action=datetime.datetime.today() - datetime.timedelta(days=1),
|
||||
)
|
||||
response = self.client.get('/test_admin/admin/admin_views/stumpjoke/add/')
|
||||
# The allowed option should appear twice; the limited option should not appear.
|
||||
self.assertContains(response, threepwood.username, count=2)
|
||||
self.assertNotContains(response, marley.username)
|
||||
|
||||
|
||||
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
|
||||
class RawIdFieldsTest(TestCase):
|
||||
urls = "admin_views.urls"
|
||||
|
||||
Reference in New Issue
Block a user