diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index d7befe4688..d10c9c11ee 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -133,7 +133,7 @@ class ForeignKeyRawIdWidget(forms.TextInput): def base_url_parameters(self): params = {} - if self.rel.limit_choices_to: + if self.rel.limit_choices_to and hasattr(self.rel.limit_choices_to, 'items'): items = [] for k, v in self.rel.limit_choices_to.items(): if isinstance(v, list): diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index d53bef2acf..db86d8e314 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -872,10 +872,10 @@ define the details of how the relation works. current date/time to be chosen. Instead of a dictionary this can also be a :class:`~django.db.models.Q` - object for more :ref:`complex queries `. - - ``limit_choices_to`` has no effect on the inline FormSets that are created - to display related objects in the admin. + object for more :ref:`complex queries `. However, + if ``limit_choices_to`` is a :class:`~django.db.models.Q` object then it + will only have an effect on the choices available in the admin when the + field is not listed in ``raw_id_fields`` in the ``ModelAdmin`` for the model. .. attribute:: ForeignKey.related_name diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py index ebf93d4b75..5632548414 100644 --- a/tests/regressiontests/admin_widgets/models.py +++ b/tests/regressiontests/admin_widgets/models.py @@ -49,7 +49,7 @@ class Inventory(models.Model): return self.name class Event(models.Model): - band = models.ForeignKey(Band) + band = models.ForeignKey(Band, limit_choices_to=models.Q(pk__gt=0)) start_date = models.DateField(blank=True, null=True) start_time = models.TimeField(blank=True, null=True) description = models.TextField(blank=True)