Ticket #2445: evallimitchoices.diff
File evallimitchoices.diff, 4.8 KB (added by , 18 years ago) |
---|
-
django/db/models/manipulators.py
264 264 # Sanity check -- Make sure the "parent" object exists. 265 265 # For example, make sure the Place exists for the Restaurant. 266 266 # Let the ObjectDoesNotExist exception propagate up. 267 limit_choices_to = self.opts.one_to_one_field.rel. limit_choices_to267 limit_choices_to = self.opts.one_to_one_field.rel.evaluate_limit_choices_to() 268 268 lookup_kwargs = {'%s__exact' % self.opts.one_to_one_field.rel.field_name: obj_key} 269 269 self.opts.one_to_one_field.rel.to.get_model_module().complex_filter(limit_choices_to).get(**lookup_kwargs) 270 270 params = dict([(f.attname, f.get_default()) for f in self.opts.fields]) -
django/db/models/fields/__init__.py
291 291 return first_choice + list(self.choices) 292 292 rel_model = self.rel.to 293 293 if hasattr(self.rel, 'get_related_field'): 294 lst = [(getattr(x, self.rel.get_related_field().attname), str(x)) for x in rel_model._default_manager.complex_filter(self.rel. limit_choices_to)]294 lst = [(getattr(x, self.rel.get_related_field().attname), str(x)) for x in rel_model._default_manager.complex_filter(self.rel.evaluate_limit_choices_to())] 295 295 else: 296 lst = [(x._get_pk_val(), str(x)) for x in rel_model._default_manager.complex_filter(self.rel. limit_choices_to)]296 lst = [(x._get_pk_val(), str(x)) for x in rel_model._default_manager.complex_filter(self.rel.evaluate_limit_choices_to())] 297 297 return first_choice + lst 298 298 299 299 def get_choices_default(self): -
django/db/models/fields/related.py
450 450 for obj in value: 451 451 manager.add(obj) 452 452 453 class ForeignKey(RelatedField, Field): 453 class LimitedChoices(object): 454 def evaluate_limit_choices_to(self): 455 limiters = {} 456 if self.limit_choices_to: 457 for id in self.limit_choices_to: 458 value = self.limit_choices_to[id] 459 if callable(value): 460 value = value() 461 limiters[id] = value 462 return limiters 463 464 class ForeignKey(RelatedField, Field, LimitedChoices): 454 465 empty_strings_allowed = False 455 466 def __init__(self, to, to_field=None, **kwargs): 456 467 try: … … 600 611 if not cls._meta.one_to_one_field: 601 612 cls._meta.one_to_one_field = self 602 613 603 class ManyToManyField(RelatedField, Field ):614 class ManyToManyField(RelatedField, Field, LimitedChoices): 604 615 def __init__(self, to, **kwargs): 605 616 kwargs['verbose_name'] = kwargs.get('verbose_name', None) 606 617 kwargs['rel'] = ManyToManyRel(to, … … 706 717 def set_attributes_from_rel(self): 707 718 pass 708 719 709 class ManyToOneRel( object):720 class ManyToOneRel(LimitedChoices): 710 721 def __init__(self, to, field_name, num_in_admin=3, min_num_in_admin=None, 711 722 max_num_in_admin=None, num_extra_on_change=1, edit_inline=False, 712 723 related_name=None, limit_choices_to=None, lookup_overrides=None, raw_id_admin=False): … … 729 740 "Returns the Field in the 'to' object to which this relationship is tied." 730 741 return self.to._meta.get_field(self.field_name) 731 742 732 class OneToOneRel(ManyToOneRel ):743 class OneToOneRel(ManyToOneRel,LimitedChoices): 733 744 def __init__(self, to, field_name, num_in_admin=0, edit_inline=False, 734 745 related_name=None, limit_choices_to=None, lookup_overrides=None, 735 746 raw_id_admin=False): … … 743 754 self.raw_id_admin = raw_id_admin 744 755 self.multiple = False 745 756 746 class ManyToManyRel( object):757 class ManyToManyRel(LimitedChoices): 747 758 def __init__(self, to, num_in_admin=0, related_name=None, 748 759 filter_interface=None, limit_choices_to=None, raw_id_admin=False, symmetrical=True): 749 760 self.to = to -
django/contrib/admin/views/main.py
733 733 qs = qs & other_qs 734 734 735 735 if self.opts.one_to_one_field: 736 qs = qs.complex_filter(self.opts.one_to_one_field.rel. limit_choices_to)736 qs = qs.complex_filter(self.opts.one_to_one_field.rel.evaluate_limit_choices_to()) 737 737 738 738 return qs 739 739