Ticket #3247: disable_formfield_for_hidden_fields_trunk.diff

File disable_formfield_for_hidden_fields_trunk.diff, 2.9 KB (added by Philipp Keller <philipp.keller@…>, 18 years ago)

Patch matching 4451, included patch as well

  • django/newforms/models.py

     
    3131        raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name)
    3232    clean_data = form.clean_data
    3333    for f in opts.fields:
    34         if isinstance(f, models.AutoField):
     34        if not f.editable or isinstance(f, models.AutoField):
    3535            continue
    3636        setattr(instance, f.attname, clean_data[f.name])
    3737    if commit:
     
    6363    opts = model._meta
    6464    field_list = []
    6565    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))
    6970    fields = SortedDictFromList(field_list)
    7071    return type(opts.object_name + 'Form', (form,), {'base_fields': fields, '_model': model, 'save': model_save})
    7172
     
    8485    opts = model._meta
    8586    field_list = []
    8687    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))
    9193    fields = SortedDictFromList(field_list)
    9294    return type(opts.object_name + 'InstanceForm', (form,),
    9395        {'base_fields': fields, '_model': model, 'save': make_instance_save(instance)})
    9496
    9597def form_for_fields(field_list):
    9698    "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])
    98100    return type('FormForFields', (BaseForm,), {'base_fields': fields})
  • tests/modeltests/model_forms/models.py

     
    4040class Article(models.Model):
    4141    headline = models.CharField(maxlength=50)
    4242    pub_date = models.DateField()
     43    created = models.DateField(editable=False)
    4344    writer = models.ForeignKey(Writer)
    4445    article = models.TextField()
    4546    categories = models.ManyToManyField(Category, blank=True)
    4647
     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
    4754    def __str__(self):
    4855        return self.headline
    4956
Back to Top