Django

Code

Changeset 8806

Show
Ignore:
Timestamp:
09/01/08 14:20:03 (3 months ago)
Author:
jacob
Message:

Repaired an oversight from [8772] that let made certain types of fields with choices fail. Fixes #6967 again.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/__init__.py

    r8772 r8806  
    305305        "Returns a django.forms.Field instance for this database Field." 
    306306        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} 
     307        if self.has_default(): 
     308            defaults['initial'] = self.get_default() 
     309 
    307310        if self.choices: 
    308             form_class = forms.TypedChoiceField 
     311            # Fields with choices get special treatment.  
    309312            include_blank = self.blank or not (self.has_default() or 'initial' in kwargs) 
    310313            defaults['choices'] = self.get_choices(include_blank=include_blank) 
     
    312315            if self.null: 
    313316                defaults['empty_value'] = None 
    314             kwargs.pop('max_length', None) 
    315         if self.has_default(): 
    316             defaults['initial'] = self.get_default() 
     317             
     318            form_class = forms.TypedChoiceField 
     319             
     320            # Many of the subclass-specific formfield arguments (min_value, 
     321            # max_value) don't apply for choice fields, so be sure to only pass 
     322            # the values that TypedChoiceField will understand. 
     323            for k in kwargs.keys(): 
     324                if k not in ('coerce', 'empty_value', 'choices', 'required', 
     325                             'widget', 'label', 'initial', 'help_text', 
     326                             'error_messages'): 
     327                    del kwargs[k] 
     328         
    317329        defaults.update(kwargs) 
    318330        return form_class(**defaults) 
  • django/trunk/tests/modeltests/model_forms/models.py

    r8805 r8806  
    5656    article = models.TextField() 
    5757    categories = models.ManyToManyField(Category, blank=True) 
    58     status = models.IntegerField(choices=ARTICLE_STATUS, blank=True, null=True) 
     58    status = models.PositiveIntegerField(choices=ARTICLE_STATUS, blank=True, null=True) 
    5959 
    6060    def save(self):