Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms-admin: Fixed #5383.

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@6102 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit addbe83dccd234f66a120c8abfef6d1e921cf7c3 1 parent 3d85b27
Joseph Kocherhans authored September 12, 2007

Showing 1 changed file with 48 additions and 19 deletions. Show diff stats Hide diff stats

  1. 67  django/contrib/admin/options.py
67  django/contrib/admin/options.py
@@ -179,22 +179,21 @@ def formfield_for_dbfield(self, db_field, **kwargs):
179 179
         # For any other type of field, just call its formfield() method.
180 180
         return db_field.formfield(**kwargs)
181 181
 
182  
-    def _fieldsets(self, request):
  182
+    def _declared_fieldsets(self):
183 183
         if self.fieldsets:
184 184
             return self.fieldsets
185  
-        if self.fields:
  185
+        elif self.fields:
186 186
             return [(None, {'fields': self.fields})]
187  
-        # TODO: switch this to pull from the form, not the model
188  
-        fields = [f.name for f in self.opts.fields + self.opts.many_to_many if f.editable and not isinstance(f, models.AutoField)]
189  
-        return [(None, {'fields': fields})]
  187
+        return None
  188
+    declared_fieldsets = property(_declared_fieldsets)
190 189
 
191 190
     def fieldsets_add(self, request):
192 191
         "Hook for specifying fieldsets for the add form."
193  
-        return list(self._fieldsets(request))
194  
-
  192
+        raise NotImplementedError
  193
+    
195 194
     def fieldsets_change(self, request, obj):
196 195
         "Hook for specifying fieldsets for the change form."
197  
-        return list(self._fieldsets(request))
  196
+        raise NotImplementedError
198 197
 
199 198
 class ModelAdmin(BaseModelAdmin):
200 199
     "Encapsulates all admin options and functionality for a given model."
@@ -309,18 +308,38 @@ def queryset_change(self, request):
309 308
         """
310 309
         return self.queryset(request)
311 310
 
  311
+    def fieldsets_add(self, request):
  312
+        "Hook for specifying fieldsets for the add form."
  313
+        if self.declared_fieldsets:
  314
+            return self.declared_fieldsets
  315
+        form = self.form_add(request)
  316
+        return [(None, {'fields': form.base_fields.keys()})]
  317
+
  318
+    def fieldsets_change(self, request, obj):
  319
+        "Hook for specifying fieldsets for the change form."
  320
+        if self.declared_fieldsets:
  321
+            return self.declared_fieldsets
  322
+        form = self.form_change(request, obj)
  323
+        return [(None, {'fields': form.base_fields.keys()})]
  324
+
312 325
     def form_add(self, request):
313 326
         """
314 327
         Returns a Form class for use in the admin add view.
315 328
         """
316  
-        fields = flatten_fieldsets(self.fieldsets_add(request))
  329
+        if self.declared_fieldsets:
  330
+            fields = flatten_fieldsets(self.declared_fieldsets)
  331
+        else:
  332
+            fields = None
317 333
         return forms.form_for_model(self.model, fields=fields, formfield_callback=self.formfield_for_dbfield)
318 334
 
319 335
     def form_change(self, request, obj):
320 336
         """
321 337
         Returns a Form class for use in the admin change view.
322 338
         """
323  
-        fields = flatten_fieldsets(self.fieldsets_change(request, obj))
  339
+        if self.declared_fieldsets:
  340
+            fields = flatten_fieldsets(self.declared_fieldsets)
  341
+        else:
  342
+            fields = None
324 343
         return forms.form_for_instance(obj, fields=fields, formfield_callback=self.formfield_for_dbfield)
325 344
 
326 345
     def save_add(self, request, model, form, formsets, post_url_continue):
@@ -678,21 +697,31 @@ def __init__(self, parent_model, admin_site):
678 697
 
679 698
     def formset_add(self, request):
680 699
         """Returns an InlineFormSet class for use in admin add views."""
681  
-        fields = flatten_fieldsets(self.fieldsets_add(request))
  700
+        if self.declared_fieldsets:
  701
+            fields = flatten_fieldsets(self.declared_fieldsets)
  702
+        else:
  703
+            fields = None
682 704
         return forms.inline_formset(self.parent_model, self.model, fk_name=self.fk_name, fields=fields, formfield_callback=self.formfield_for_dbfield, extra=self.extra)
683 705
 
684 706
     def formset_change(self, request, obj):
685 707
         """Returns an InlineFormSet class for use in admin change views."""
686  
-        fields = flatten_fieldsets(self.fieldsets_change(request, obj))
  708
+        if self.declared_fieldsets:
  709
+            fields = flatten_fieldsets(self.declared_fieldsets)
  710
+        else:
  711
+            fields = None
687 712
         return forms.inline_formset(self.parent_model, self.model, fk_name=self.fk_name, fields=fields, formfield_callback=self.formfield_for_dbfield, extra=self.extra)
688 713
 
689  
-    def _fieldsets(self, request):
690  
-        if self.fieldsets:
691  
-            return self.fieldsets
692  
-        if self.fields:
693  
-            return [(None, {'fields': self.fields})]
694  
-        fields = [f for f in self.formset_class(request).form_class.base_fields.keys()]
695  
-        return [(None, {'fields': fields})]
  714
+    def fieldsets_add(self, request):
  715
+        if self.declared_fieldsets:
  716
+            return self.declared_fieldsets
  717
+        form = self.formset_add(request).form_class
  718
+        return [(None, {'fields': form.base_fields.keys()})]
  719
+
  720
+    def fieldsets_change(self, request, obj):
  721
+        if self.declared_fieldsets:
  722
+            return self.declared_fieldsets
  723
+        form = self.formset_change(request, obj).form_class
  724
+        return [(None, {'fields': form.base_fields.keys()})]
696 725
 
697 726
 class StackedInline(InlineModelAdmin):
698 727
     template = 'admin/edit_inline_stacked.html'

0 notes on commit addbe83

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