==== 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
==================================================================
|
|
|
344 | 344 | def formfield(self, **kwargs): |
345 | 345 | "Returns a django.newforms.Field instance for this database Field." |
346 | 346 | 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() |
347 | 349 | defaults.update(kwargs) |
348 | 350 | return forms.CharField(**defaults) |
349 | 351 | |
… |
… |
|
406 | 408 | |
407 | 409 | def formfield(self, **kwargs): |
408 | 410 | 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() |
409 | 413 | defaults.update(kwargs) |
410 | 414 | return forms.BooleanField(**defaults) |
411 | 415 | |
… |
… |
|
425 | 429 | |
426 | 430 | def formfield(self, **kwargs): |
427 | 431 | 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() |
428 | 434 | defaults.update(kwargs) |
429 | 435 | return forms.CharField(**defaults) |
430 | 436 | |
… |
… |
|
503 | 509 | |
504 | 510 | def formfield(self, **kwargs): |
505 | 511 | 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() |
506 | 514 | defaults.update(kwargs) |
507 | 515 | return forms.DateField(**defaults) |
508 | 516 | |
… |
… |
|
568 | 576 | |
569 | 577 | def formfield(self, **kwargs): |
570 | 578 | 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() |
571 | 581 | defaults.update(kwargs) |
572 | 582 | return forms.DateTimeField(**defaults) |
573 | 583 | |
… |
… |
|
587 | 597 | |
588 | 598 | def formfield(self, **kwargs): |
589 | 599 | 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() |
590 | 602 | defaults.update(kwargs) |
591 | 603 | return forms.EmailField(**defaults) |
592 | 604 | |
… |
… |
|
724 | 736 | |
725 | 737 | def formfield(self, **kwargs): |
726 | 738 | 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() |
727 | 741 | defaults.update(kwargs) |
728 | 742 | return forms.IntegerField(**defaults) |
729 | 743 | |
… |
… |
|
763 | 777 | def formfield(self, **kwargs): |
764 | 778 | from django.contrib.localflavor.us.forms import USPhoneNumberField |
765 | 779 | 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() |
766 | 782 | defaults.update(kwargs) |
767 | 783 | return USPhoneNumberField(**defaults) |
768 | 784 | |
… |
… |
|
796 | 812 | |
797 | 813 | def formfield(self, **kwargs): |
798 | 814 | 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() |
799 | 817 | defaults.update(kwargs) |
800 | 818 | return forms.CharField(**defaults) |
801 | 819 | |
… |
… |
|
841 | 859 | |
842 | 860 | def formfield(self, **kwargs): |
843 | 861 | 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() |
844 | 864 | defaults.update(kwargs) |
845 | 865 | return forms.TimeField(**defaults) |
846 | 866 | |
… |
… |
|
860 | 880 | |
861 | 881 | def formfield(self, **kwargs): |
862 | 882 | 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() |
863 | 885 | defaults.update(kwargs) |
864 | 886 | return forms.URLField(**defaults) |
865 | 887 | |
=== django/db/models/fields/related.py
==================================================================
|
|
|
554 | 554 | |
555 | 555 | def formfield(self, **kwargs): |
556 | 556 | 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() |
557 | 559 | defaults.update(kwargs) |
558 | 560 | return forms.ModelChoiceField(**defaults) |
559 | 561 | |
… |
… |
|
620 | 622 | |
621 | 623 | def formfield(self, **kwargs): |
622 | 624 | 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() |
623 | 627 | defaults.update(kwargs) |
624 | 628 | return forms.ModelChoiceField(**defaults) |
625 | 629 | |
… |
… |
|
743 | 747 | if kwargs.get('initial') is not None: |
744 | 748 | kwargs['initial'] = [i._get_pk_val() for i in kwargs['initial']] |
745 | 749 | 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() |
746 | 752 | defaults.update(kwargs) |
747 | 753 | return forms.ModelMultipleChoiceField(**defaults) |
748 | 754 | |