Ticket #3247: disable_formfield_for_hidden_fields_trunk.diff
File disable_formfield_for_hidden_fields_trunk.diff, 2.9 KB (added by , 18 years ago) |
---|
-
django/newforms/models.py
31 31 raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) 32 32 clean_data = form.clean_data 33 33 for f in opts.fields: 34 if isinstance(f, models.AutoField):34 if not f.editable or isinstance(f, models.AutoField): 35 35 continue 36 36 setattr(instance, f.attname, clean_data[f.name]) 37 37 if commit: … … 63 63 opts = model._meta 64 64 field_list = [] 65 65 for f in opts.fields + opts.many_to_many: 66 formfield = formfield_callback(f) 67 if formfield: 68 field_list.append((f.name, formfield)) 66 if f.editable: 67 formfield = formfield_callback(f) 68 if formfield: 69 field_list.append((f.name, formfield)) 69 70 fields = SortedDictFromList(field_list) 70 71 return type(opts.object_name + 'Form', (form,), {'base_fields': fields, '_model': model, 'save': model_save}) 71 72 … … 84 85 opts = model._meta 85 86 field_list = [] 86 87 for f in opts.fields + opts.many_to_many: 87 current_value = f.value_from_object(instance) 88 formfield = formfield_callback(f, initial=current_value) 89 if formfield: 90 field_list.append((f.name, formfield)) 88 if f.editable: 89 current_value = f.value_from_object(instance) 90 formfield = formfield_callback(f, initial=current_value) 91 if formfield: 92 field_list.append((f.name, formfield)) 91 93 fields = SortedDictFromList(field_list) 92 94 return type(opts.object_name + 'InstanceForm', (form,), 93 95 {'base_fields': fields, '_model': model, 'save': make_instance_save(instance)}) 94 96 95 97 def form_for_fields(field_list): 96 98 "Returns a Form class for the given list of Django database field instances." 97 fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list ])99 fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list if f.editable]) 98 100 return type('FormForFields', (BaseForm,), {'base_fields': fields}) -
tests/modeltests/model_forms/models.py
40 40 class Article(models.Model): 41 41 headline = models.CharField(maxlength=50) 42 42 pub_date = models.DateField() 43 created = models.DateField(editable=False) 43 44 writer = models.ForeignKey(Writer) 44 45 article = models.TextField() 45 46 categories = models.ManyToManyField(Category, blank=True) 46 47 48 def save(self): 49 import datetime 50 if not self.id: 51 self.created = datetime.date.today() 52 return super(Article, self).save() 53 47 54 def __str__(self): 48 55 return self.headline 49 56