Ticket #899: initial_formfield.patch

File initial_formfield.patch, 6.3 KB (added by antisvin@…, 8 years ago)

Use get_default model fields method to set initial values for forms.

  • home/antisvin/svn/django/django/db/models/fields/__init__.py

     
    343343
    344344    def formfield(self, **kwargs):
    345345        "Returns a django.newforms.Field instance for this database Field."
    346         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     346        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    347347        defaults.update(kwargs)
    348348        return forms.CharField(**defaults)
    349349
     
    405405        return [oldforms.CheckboxField]
    406406
    407407    def formfield(self, **kwargs):
    408         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     408        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    409409        defaults.update(kwargs)
    410410        return forms.BooleanField(**defaults)
    411411
     
    424424        return str(value)
    425425
    426426    def formfield(self, **kwargs):
    427         defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     427        defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    428428        defaults.update(kwargs)
    429429        return forms.CharField(**defaults)
    430430
     
    502502        return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or '')}
    503503
    504504    def formfield(self, **kwargs):
    505         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     505        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    506506        defaults.update(kwargs)
    507507        return forms.DateField(**defaults)
    508508
     
    567567                time_field: (val is not None and val.strftime("%H:%M:%S") or '')}
    568568
    569569    def formfield(self, **kwargs):
    570         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     570        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    571571        defaults.update(kwargs)
    572572        return forms.DateTimeField(**defaults)
    573573
     
    586586        validators.isValidEmail(field_data, all_data)
    587587
    588588    def formfield(self, **kwargs):
    589         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     589        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    590590        defaults.update(kwargs)
    591591        return forms.EmailField(**defaults)
    592592
     
    723723        return [oldforms.IntegerField]
    724724
    725725    def formfield(self, **kwargs):
    726         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     726        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    727727        defaults.update(kwargs)
    728728        return forms.IntegerField(**defaults)
    729729
     
    762762
    763763    def formfield(self, **kwargs):
    764764        from django.contrib.localflavor.usa.forms import USPhoneNumberField
    765         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     765        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    766766        defaults.update(kwargs)
    767767        return USPhoneNumberField(**defaults)
    768768
     
    795795        return [oldforms.LargeTextField]
    796796
    797797    def formfield(self, **kwargs):
    798         defaults = {'required': not self.blank, 'widget': forms.Textarea, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     798        defaults = {'required': not self.blank, 'widget': forms.Textarea, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    799799        defaults.update(kwargs)
    800800        return forms.CharField(**defaults)
    801801
     
    840840        return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')}
    841841
    842842    def formfield(self, **kwargs):
    843         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     843        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    844844        defaults.update(kwargs)
    845845        return forms.TimeField(**defaults)
    846846
  • home/antisvin/svn/django/django/db/models/fields/related.py

     
    553553        setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related))
    554554
    555555    def formfield(self, **kwargs):
    556         defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     556        defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    557557        defaults.update(kwargs)
    558558        return forms.ModelChoiceField(**defaults)
    559559
     
    619619            cls._meta.one_to_one_field = self
    620620
    621621    def formfield(self, **kwargs):
    622         defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     622        defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text, 'initial': self.get_default()}
    623623        defaults.update(kwargs)
    624624        return forms.ModelChoiceField(**defaults)
    625625
Back to Top