Ticket #2445: choices_func_newforms-admin.patch
File choices_func_newforms-admin.patch, 2.5 KB (added by , 17 years ago) |
---|
-
django/db/models/base.py
165 165 val = field.get_default() 166 166 setattr(self, field.attname, val) 167 167 168 for item in dir(self): 169 if item.startswith('choices_for__'): 170 choice_func = getattr(self,item) 171 if callable(choice_func): 172 field_name = choice_func.__name__[len('choices_for__'):] 173 self._meta.get_field(field_name)._choices = choice_func 174 168 175 if kwargs: 169 176 for prop in kwargs.keys(): 170 177 try: -
django/db/models/fields/__init__.py
8 8 9 9 from django.db import get_creation_module 10 10 from django.db.models import signals 11 import django.db.models.query 11 12 from django.dispatch import dispatcher 12 13 from django.conf import settings 13 14 from django.core import validators … … 338 339 "Returns a list of tuples used as SelectField choices for this field." 339 340 first_choice = include_blank and blank_choice or [] 340 341 if self.choices: 341 return first_choice + list(self.choices) 342 if callable(self.choices): 343 choices = self.choices() 344 else: 345 choices = self.choices 346 # If choices is a QuerySet, convert into a tuple list ((pk,__unicode__),etc) 347 if isinstance(choices, django.db.models.query.QuerySet): 348 choices = [(x._get_pk_val(), smart_unicode(x)) for x in choices] 349 return first_choice + list(choices) 350 342 351 rel_model = self.rel.to 343 352 if hasattr(self.rel, 'get_related_field'): 344 353 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)] … … 391 400 defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 392 401 if self.choices: 393 402 defaults['widget'] = forms.Select(choices=self.get_choices(include_blank=self.blank or not (self.has_default() or 'initial' in kwargs))) 403 kwargs['queryset'] = self.choices() 394 404 if self.has_default(): 395 405 defaults['initial'] = self.get_default() 396 406 defaults.update(kwargs)