Ticket #1891: limit_choice_to.diff

File limit_choice_to.diff, 5.8 KB (added by Alex Gaynor, 17 years ago)

Patch against qs-rf

  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index a295edf..586100b 100644
    a b except ImportError:  
    88
    99from django.db import get_creation_module
    1010from django.db.models import signals
     11import django.db.models.query
    1112from django.dispatch import dispatcher
    1213from django.conf import settings
    1314from django.core import validators
    class Field(object):  
    359360        "Returns a list of tuples used as SelectField choices for this field."
    360361        first_choice = include_blank and blank_choice or []
    361362        if self.choices:
    362             return first_choice + list(self.choices)
     363            if callable(self.choices):
     364                choices = list(self.choices())
     365            elif isinstance(self.choices, django.db.models.query.QuerySet):
     366                choices = [(x._get_pk_val(), smart_unicode(x)) for x in self.choices]
     367            else:
     368                choices = list(self.choices)
     369            return first_choice + choices
    363370        rel_model = self.rel.to
    364371        if hasattr(self.rel, 'get_related_field'):
    365             lst = [(getattr(x, self.rel.get_related_field().attname), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
     372            lst = [(getattr(x, self.rel.get_related_field().attname), smart_unicode(x)) for x in rel_model._default_manager.all()]
    366373        else:
    367             lst = [(x._get_pk_val(), smart_unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
     374            lst = [(x._get_pk_val(), smart_unicode(x)) for x in rel_model._default_manager.all()]
    368375        return first_choice + lst
    369376
    370377    def get_choices_default(self):
  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 818c8a9..a596bec 100644
    a b class ReverseManyRelatedObjectsDescriptor(object):  
    506506class ManyToOneRel(object):
    507507    def __init__(self, to, field_name, num_in_admin=3, min_num_in_admin=None,
    508508            max_num_in_admin=None, num_extra_on_change=1, edit_inline=False,
    509             related_name=None, limit_choices_to=None, lookup_overrides=None,
     509            related_name=None, choices=None, lookup_overrides=None,
    510510            raw_id_admin=False, parent_link=False):
    511511        try:
    512512            to._meta
    class ManyToOneRel(object):  
    516516        self.num_in_admin, self.edit_inline = num_in_admin, edit_inline
    517517        self.min_num_in_admin, self.max_num_in_admin = min_num_in_admin, max_num_in_admin
    518518        self.num_extra_on_change, self.related_name = num_extra_on_change, related_name
    519         if limit_choices_to is None:
    520             limit_choices_to = {}
    521         self.limit_choices_to = limit_choices_to
     519        self.choices = choices
    522520        self.lookup_overrides = lookup_overrides or {}
    523521        self.raw_id_admin = raw_id_admin
    524522        self.multiple = True
    class ManyToOneRel(object):  
    534532class OneToOneRel(ManyToOneRel):
    535533    def __init__(self, to, field_name, num_in_admin=0, min_num_in_admin=None,
    536534            max_num_in_admin=None, num_extra_on_change=None, edit_inline=False,
    537             related_name=None, limit_choices_to=None, lookup_overrides=None,
     535            related_name=None, choices=None, lookup_overrides=None,
    538536            raw_id_admin=False, parent_link=False):
    539537        # NOTE: *_num_in_admin and num_extra_on_change are intentionally
    540538        # ignored here. We accept them as parameters only to match the calling
    541539        # signature of ManyToOneRel.__init__().
    542540        super(OneToOneRel, self).__init__(to, field_name, num_in_admin,
    543541                edit_inline=edit_inline, related_name=related_name,
    544                 limit_choices_to=limit_choices_to,
     542                choices=choices,
    545543                lookup_overrides=lookup_overrides, raw_id_admin=raw_id_admin,
    546544                parent_link=parent_link)
    547545        self.multiple = False
    548546
    549547class ManyToManyRel(object):
    550548    def __init__(self, to, num_in_admin=0, related_name=None,
    551         filter_interface=None, limit_choices_to=None, raw_id_admin=False, symmetrical=True):
     549        filter_interface=None, choices=None, raw_id_admin=False, symmetrical=True):
    552550        self.to = to
    553551        self.num_in_admin = num_in_admin
    554552        self.related_name = related_name
    555553        self.filter_interface = filter_interface
    556         if limit_choices_to is None:
    557             limit_choices_to = {}
    558         self.limit_choices_to = limit_choices_to
     554        self.choices = choices
    559555        self.edit_inline = False
    560556        self.raw_id_admin = raw_id_admin
    561557        self.symmetrical = symmetrical
    class ForeignKey(RelatedField, Field):  
    586582            num_extra_on_change=kwargs.pop('num_extra_on_change', 1),
    587583            edit_inline=kwargs.pop('edit_inline', False),
    588584            related_name=kwargs.pop('related_name', None),
    589             limit_choices_to=kwargs.pop('limit_choices_to', None),
     585            choices=kwargs.pop('choices', None),
    590586            lookup_overrides=kwargs.pop('lookup_overrides', None),
    591587            raw_id_admin=kwargs.pop('raw_id_admin', False),
    592588            parent_link=kwargs.pop('parent_link', False))
    class ManyToManyField(RelatedField, Field):  
    694690            num_in_admin=kwargs.pop('num_in_admin', 0),
    695691            related_name=kwargs.pop('related_name', None),
    696692            filter_interface=kwargs.pop('filter_interface', None),
    697             limit_choices_to=kwargs.pop('limit_choices_to', None),
     693            choices=kwargs.pop('choices', None),
    698694            raw_id_admin=kwargs.pop('raw_id_admin', False),
    699695            symmetrical=kwargs.pop('symmetrical', True))
    700696        self.db_table = kwargs.pop('db_table', None)
Back to Top