Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3247 -- newforms form_for_model() and form_for_instance() no l…

…onger create form fields for database fields with editable=False. Thanks for the patch, mssnlayam@yahoo.com and Philipp Keller

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4548 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bdfbcb2cd51643d2d866eda8820b62521901a007 1 parent e56934b
Adrian Holovaty authored February 20, 2007
8  django/newforms/models.py
@@ -34,7 +34,7 @@ def save_instance(form, instance, commit=True):
34 34
         raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name)
35 35
     clean_data = form.clean_data
36 36
     for f in opts.fields:
37  
-        if isinstance(f, models.AutoField):
  37
+        if not f.editable or isinstance(f, models.AutoField):
38 38
             continue
39 39
         setattr(instance, f.name, clean_data[f.name])
40 40
     if commit:
@@ -66,6 +66,8 @@ def form_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfiel
66 66
     opts = model._meta
67 67
     field_list = []
68 68
     for f in opts.fields + opts.many_to_many:
  69
+        if not f.editable:
  70
+            continue
69 71
         formfield = formfield_callback(f)
70 72
         if formfield:
71 73
             field_list.append((f.name, formfield))
@@ -87,6 +89,8 @@ def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kw
87 89
     opts = model._meta
88 90
     field_list = []
89 91
     for f in opts.fields + opts.many_to_many:
  92
+        if not f.editable:
  93
+            continue
90 94
         current_value = f.value_from_object(instance)
91 95
         formfield = formfield_callback(f, initial=current_value)
92 96
         if formfield:
@@ -97,7 +101,7 @@ def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kw
97 101
 
98 102
 def form_for_fields(field_list):
99 103
     "Returns a Form class for the given list of Django database field instances."
100  
-    fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list])
  104
+    fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list if f.editable])
101 105
     return type('FormForFields', (BaseForm,), {'base_fields': fields})
102 106
 
103 107
 class ModelChoiceField(ChoiceField):
7  tests/modeltests/model_forms/models.py
@@ -40,10 +40,17 @@ def __str__(self):
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
 

0 notes on commit bdfbcb2

Please sign in to comment.
Something went wrong with that request. Please try again.