Ticket #4019: django-use-model-default-as-newforms-initial.diff

File django-use-model-default-as-newforms-initial.diff, 6.3 KB (added by David Danier <goliath.mailinglist@…>, 18 years ago)
  • django/db/models/fields/__init__.py

    ==== Patch <django-use-model-default-as-newforms-initial> level 3
    Source: 6666b74b-a72c-0410-9b74-dd6beae1e16f:/django/use-model-default-as-newforms-initial:5944 [local]
    Target: bcc190cf-cafb-0310-a4f2-bffc1f526a37:/django/trunk:4993 [mirrored]
            (http://code.djangoproject.com/svn/django/trunk)
    Log:
     r5941@damasonium:  ddanier | 2007-04-12 10:07:38 +0200
      * Make model-field-default the initial value for newforms-fields
     r5943@damasonium:  ddanier | 2007-04-12 10:14:30 +0200
      * Simplified things
     r5944@damasonium:  ddanier | 2007-04-12 10:31:45 +0200
      * Added new formfield()-method to all fields (there should really be some central method to do most of the work)
    
    === django/db/models/fields/__init__.py
    ==================================================================
     
    344344    def formfield(self, **kwargs):
    345345        "Returns a django.newforms.Field instance for this database Field."
    346346        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     347        if self.has_default():
     348            defaults['initial'] = self.get_default()
    347349        defaults.update(kwargs)
    348350        return forms.CharField(**defaults)
    349351
     
    406408
    407409    def formfield(self, **kwargs):
    408410        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     411        if self.has_default():
     412            defaults['initial'] = self.get_default()
    409413        defaults.update(kwargs)
    410414        return forms.BooleanField(**defaults)
    411415
     
    425429
    426430    def formfield(self, **kwargs):
    427431        defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     432        if self.has_default():
     433            defaults['initial'] = self.get_default()
    428434        defaults.update(kwargs)
    429435        return forms.CharField(**defaults)
    430436
     
    503509
    504510    def formfield(self, **kwargs):
    505511        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     512        if self.has_default():
     513            defaults['initial'] = self.get_default()
    506514        defaults.update(kwargs)
    507515        return forms.DateField(**defaults)
    508516
     
    568576
    569577    def formfield(self, **kwargs):
    570578        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     579        if self.has_default():
     580            defaults['initial'] = self.get_default()
    571581        defaults.update(kwargs)
    572582        return forms.DateTimeField(**defaults)
    573583
     
    587597
    588598    def formfield(self, **kwargs):
    589599        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     600        if self.has_default():
     601            defaults['initial'] = self.get_default()
    590602        defaults.update(kwargs)
    591603        return forms.EmailField(**defaults)
    592604
     
    724736
    725737    def formfield(self, **kwargs):
    726738        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     739        if self.has_default():
     740            defaults['initial'] = self.get_default()
    727741        defaults.update(kwargs)
    728742        return forms.IntegerField(**defaults)
    729743
     
    763777    def formfield(self, **kwargs):
    764778        from django.contrib.localflavor.us.forms import USPhoneNumberField
    765779        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     780        if self.has_default():
     781            defaults['initial'] = self.get_default()
    766782        defaults.update(kwargs)
    767783        return USPhoneNumberField(**defaults)
    768784
     
    796812
    797813    def formfield(self, **kwargs):
    798814        defaults = {'required': not self.blank, 'widget': forms.Textarea, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     815        if self.has_default():
     816            defaults['initial'] = self.get_default()
    799817        defaults.update(kwargs)
    800818        return forms.CharField(**defaults)
    801819
     
    841859
    842860    def formfield(self, **kwargs):
    843861        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     862        if self.has_default():
     863            defaults['initial'] = self.get_default()
    844864        defaults.update(kwargs)
    845865        return forms.TimeField(**defaults)
    846866
     
    860880
    861881    def formfield(self, **kwargs):
    862882        defaults = {'required': not self.blank, 'verify_exists': self.verify_exists, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     883        if self.has_default():
     884            defaults['initial'] = self.get_default()
    863885        defaults.update(kwargs)
    864886        return forms.URLField(**defaults)
    865887
  • django/db/models/fields/related.py

    === django/db/models/fields/related.py
    ==================================================================
     
    554554
    555555    def formfield(self, **kwargs):
    556556        defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     557        if self.has_default():
     558            defaults['initial'] = self.get_default()
    557559        defaults.update(kwargs)
    558560        return forms.ModelChoiceField(**defaults)
    559561
     
    620622
    621623    def formfield(self, **kwargs):
    622624        defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     625        if self.has_default():
     626            defaults['initial'] = self.get_default()
    623627        defaults.update(kwargs)
    624628        return forms.ModelChoiceField(**defaults)
    625629
     
    743747        if kwargs.get('initial') is not None:
    744748            kwargs['initial'] = [i._get_pk_val() for i in kwargs['initial']]
    745749        defaults = {'queryset' : self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
     750        if self.has_default():
     751            defaults['initial'] = self.get_default()
    746752        defaults.update(kwargs)
    747753        return forms.ModelMultipleChoiceField(**defaults)
    748754
Back to Top