Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms-admin: Collapsed some of the add/change hooks into a single …

…get method that takes an optional obj argument. Backwards incompatible.

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7275 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c323623361ba7c62c78413e43e1c8e745863013a 1 parent 46783a0
Joseph Kocherhans authored March 17, 2008
106  django/contrib/admin/options.py
@@ -197,18 +197,10 @@ def _declared_fieldsets(self):
197 197
         return None
198 198
     declared_fieldsets = property(_declared_fieldsets)
199 199
 
200  
-    def fieldsets_add(self, request):
201  
-        "Hook for specifying fieldsets for the add form."
202  
-        raise NotImplementedError
203  
-    
204  
-    def fieldsets_change(self, request, obj):
205  
-        "Hook for specifying fieldsets for the change form."
206  
-        raise NotImplementedError
207  
-
208 200
 class ModelAdmin(BaseModelAdmin):
209 201
     "Encapsulates all admin options and functionality for a given model."
210 202
     __metaclass__ = forms.MediaDefiningClass
211  
-    
  203
+
212 204
     list_display = ('__str__',)
213 205
     list_display_links = ()
214 206
     list_filter = ()
@@ -269,7 +261,7 @@ def _media(self):
269 261
         
270 262
         return forms.Media(js=['%s%s' % (settings.ADMIN_MEDIA_PREFIX, url) for url in js])
271 263
     media = property(_media)
272  
-    
  264
+
273 265
     def has_add_permission(self, request):
274 266
         "Returns True if the given request has permission to add an object."
275 267
         opts = self.opts
@@ -300,42 +292,22 @@ def has_delete_permission(self, request, obj=None):
300 292
     def queryset(self, request):
301 293
         """
302 294
         Returns a QuerySet of all model instances that can be edited by the
303  
-        admin site.
  295
+        admin site. This is used by changelist_view.
304 296
         """
305 297
         ordering = self.ordering or () # otherwise we might try to *None, which is bad ;)
306 298
         return self.model._default_manager.get_query_set().order_by(*ordering)
307 299
 
308  
-    def queryset_add(self, request):
309  
-        """
310  
-        Returns a QuerySet of all model instances that can be edited by the
311  
-        admin site in the "add" stage.
312  
-        """
313  
-        return self.queryset(request)
314  
-
315  
-    def queryset_change(self, request):
316  
-        """
317  
-        Returns a QuerySet of all model instances that can be edited by the
318  
-        admin site in the "change" stage.
319  
-        """
320  
-        return self.queryset(request)
321  
-
322  
-    def fieldsets_add(self, request):
  300
+    def get_fieldsets(self, request, obj=None):
323 301
         "Hook for specifying fieldsets for the add form."
324 302
         if self.declared_fieldsets:
325 303
             return self.declared_fieldsets
326  
-        form = self.form_add(request)
  304
+        form = self.get_form(request)
327 305
         return [(None, {'fields': form.base_fields.keys()})]
328 306
 
329  
-    def fieldsets_change(self, request, obj):
330  
-        "Hook for specifying fieldsets for the change form."
331  
-        if self.declared_fieldsets:
332  
-            return self.declared_fieldsets
333  
-        form = self.form_change(request, obj)
334  
-        return [(None, {'fields': form.base_fields.keys()})]
335  
-
336  
-    def form_add(self, request):
  307
+    def get_form(self, request, obj=None):
337 308
         """
338  
-        Returns a Form class for use in the admin add view.
  309
+        Returns a Form class for use in the admin add view. This is used by
  310
+        add_view and change_view.
339 311
         """
340 312
         if self.declared_fieldsets:
341 313
             fields = flatten_fieldsets(self.declared_fieldsets)
@@ -343,15 +315,9 @@ def form_add(self, request):
343 315
             fields = None
344 316
         return _modelform_factory(self.model, fields=fields, formfield_callback=self.formfield_for_dbfield)
345 317
 
346  
-    def form_change(self, request, obj):
347  
-        """
348  
-        Returns a Form class for use in the admin change view.
349  
-        """
350  
-        if self.declared_fieldsets:
351  
-            fields = flatten_fieldsets(self.declared_fieldsets)
352  
-        else:
353  
-            fields = None
354  
-        return _modelform_factory(self.model, fields=fields, formfield_callback=self.formfield_for_dbfield)
  318
+    def get_formsets(self, request, obj=None):
  319
+        for inline in self.inline_instances:
  320
+            yield inline.get_formset(request, obj)
355 321
 
356 322
     def save_add(self, request, model, form, formsets, post_url_continue):
357 323
         """
@@ -491,30 +457,30 @@ def add_view(self, request, form_url=''):
491 457
             # Object list will give 'Permission Denied', so go back to admin home
492 458
             post_url = '../../../'
493 459
 
494  
-        ModelForm = self.form_add(request)
  460
+        ModelForm = self.get_form(request)
495 461
         inline_formsets = []
496 462
         obj = self.model()
497 463
         if request.method == 'POST':
498 464
             form = ModelForm(request.POST, request.FILES)
499  
-            for FormSet in self.formsets_add(request):
  465
+            for FormSet in self.get_formsets(request):
500 466
                 inline_formset = FormSet(data=request.POST, files=request.FILES, instance=obj)
501 467
                 inline_formsets.append(inline_formset)
502 468
             if all_valid(inline_formsets) and form.is_valid():
503 469
                 return self.save_add(request, model, form, inline_formsets, '../%s/')
504 470
         else:
505 471
             form = ModelForm(initial=request.GET)
506  
-            for FormSet in self.formsets_add(request):
  472
+            for FormSet in self.get_formsets(request):
507 473
                 inline_formset = FormSet(instance=obj)
508 474
                 inline_formsets.append(inline_formset)
509 475
 
510  
-        adminForm = AdminForm(form, list(self.fieldsets_add(request)), self.prepopulated_fields)
  476
+        adminForm = AdminForm(form, list(self.get_fieldsets(request)), self.prepopulated_fields)
511 477
         media = self.media + adminForm.media
512 478
         for fs in inline_formsets:
513 479
             media = media + fs.media
514 480
 
515 481
         inline_admin_formsets = []
516 482
         for inline, formset in zip(self.inline_instances, inline_formsets):
517  
-            fieldsets = list(inline.fieldsets_add(request))
  483
+            fieldsets = list(inline.get_fieldsets(request))
518 484
             inline_admin_formset = InlineAdminFormSet(inline, formset, fieldsets)
519 485
             inline_admin_formsets.append(inline_admin_formset)
520 486
 
@@ -551,11 +517,11 @@ def change_view(self, request, object_id):
551 517
         if request.POST and request.POST.has_key("_saveasnew"):
552 518
             return self.add_view(request, form_url='../../add/')
553 519
 
554  
-        ModelForm = self.form_change(request, obj)
  520
+        ModelForm = self.get_form(request, obj)
555 521
         inline_formsets = []
556 522
         if request.method == 'POST':
557 523
             form = ModelForm(request.POST, request.FILES, instance=obj)
558  
-            for FormSet in self.formsets_change(request, obj):
  524
+            for FormSet in self.get_formsets(request, obj):
559 525
                 inline_formset = FormSet(request.POST, request.FILES, instance=obj)
560 526
                 inline_formsets.append(inline_formset)
561 527
 
@@ -563,7 +529,7 @@ def change_view(self, request, object_id):
563 529
                 return self.save_change(request, model, form, inline_formsets)
564 530
         else:
565 531
             form = ModelForm(instance=obj)
566  
-            for FormSet in self.formsets_change(request, obj):
  532
+            for FormSet in self.get_formsets(request, obj):
567 533
                 inline_formset = FormSet(instance=obj)
568 534
                 inline_formsets.append(inline_formset)
569 535
 
@@ -581,14 +547,14 @@ def change_view(self, request, object_id):
581 547
                 #orig_list = func()
582 548
                 #oldform.order_objects.extend(orig_list)
583 549
                 
584  
-        adminForm = AdminForm(form, self.fieldsets_change(request, obj), self.prepopulated_fields)
  550
+        adminForm = AdminForm(form, self.get_fieldsets(request, obj), self.prepopulated_fields)
585 551
         media = self.media + adminForm.media
586 552
         for fs in inline_formsets:
587 553
             media = media + fs.media
588 554
 
589 555
         inline_admin_formsets = []
590 556
         for inline, formset in zip(self.inline_instances, inline_formsets):
591  
-            fieldsets = list(inline.fieldsets_change(request, obj))
  557
+            fieldsets = list(inline.get_fieldsets(request, obj))
592 558
             inline_admin_formset = InlineAdminFormSet(inline, formset, fieldsets)
593 559
             inline_admin_formsets.append(inline_admin_formset)
594 560
 
@@ -702,14 +668,6 @@ def history_view(self, request, object_id):
702 668
         ]
703 669
         return render_to_response(template_list, extra_context, context_instance=template.RequestContext(request))
704 670
 
705  
-    def formsets_add(self, request):
706  
-        for inline in self.inline_instances:
707  
-            yield inline.formset_add(request)
708  
-
709  
-    def formsets_change(self, request, obj):
710  
-        for inline in self.inline_instances:
711  
-            yield inline.formset_change(request, obj)
712  
-
713 671
 class InlineModelAdmin(BaseModelAdmin):
714 672
     """
715 673
     Options for inline editing of ``model`` instances.
@@ -735,32 +693,18 @@ def __init__(self, parent_model, admin_site):
735 693
         if self.verbose_name_plural is None:
736 694
             self.verbose_name_plural = self.model._meta.verbose_name_plural
737 695
 
738  
-    def formset_add(self, request):
739  
-        """Returns an InlineFormSet class for use in admin add views."""
  696
+    def get_formset(self, request, obj=None):
  697
+        """Returns a BaseInlineFormSet class for use in admin add/change views."""
740 698
         if self.declared_fieldsets:
741 699
             fields = flatten_fieldsets(self.declared_fieldsets)
742 700
         else:
743 701
             fields = None
744 702
         return _inlineformset_factory(self.parent_model, self.model, fk_name=self.fk_name, fields=fields, formfield_callback=self.formfield_for_dbfield, extra=self.extra)
745 703
 
746  
-    def formset_change(self, request, obj):
747  
-        """Returns an InlineFormSet class for use in admin change views."""
748  
-        if self.declared_fieldsets:
749  
-            fields = flatten_fieldsets(self.declared_fieldsets)
750  
-        else:
751  
-            fields = None
752  
-        return _inlineformset_factory(self.parent_model, self.model, fk_name=self.fk_name, fields=fields, formfield_callback=self.formfield_for_dbfield, extra=self.extra)
753  
-
754  
-    def fieldsets_add(self, request):
755  
-        if self.declared_fieldsets:
756  
-            return self.declared_fieldsets
757  
-        form = self.formset_add(request).form
758  
-        return [(None, {'fields': form.base_fields.keys()})]
759  
-
760  
-    def fieldsets_change(self, request, obj):
  704
+    def get_fieldsets(self, request, obj=None):
761 705
         if self.declared_fieldsets:
762 706
             return self.declared_fieldsets
763  
-        form = self.formset_change(request, obj).form
  707
+        form = self.get_formset(request).form
764 708
         return [(None, {'fields': form.base_fields.keys()})]
765 709
 
766 710
 class StackedInline(InlineModelAdmin):
2  django/contrib/admin/views/main.py
@@ -120,7 +120,7 @@ def __init__(self, request, model, list_display, list_display_links, list_filter
120 120
         self.model = model
121 121
         self.opts = model._meta
122 122
         self.lookup_opts = self.opts
123  
-        self.root_query_set = model_admin.queryset_change(request)
  123
+        self.root_query_set = model_admin.queryset(request)
124 124
         self.list_display = list_display
125 125
         self.list_display_links = list_display_links
126 126
         self.list_filter = list_filter
18  tests/regressiontests/modeladmin/models.py
@@ -25,7 +25,7 @@ class and an AdminSite instance, so let's just go ahead and do that manually
25 25
 >>> site = AdminSite()
26 26
 >>> ma = ModelAdmin(Band, site)
27 27
 
28  
->>> ma.form_add(request).base_fields.keys()
  28
+>>> ma.get_form(request).base_fields.keys()
29 29
 ['name', 'bio']
30 30
 
31 31
 
@@ -39,9 +39,9 @@ class and an AdminSite instance, so let's just go ahead and do that manually
39 39
 no fields argument, and no fieldsets argument.
40 40
 
41 41
 >>> ma = ModelAdmin(Band, site)
42  
->>> ma.fieldsets_add(request)
  42
+>>> ma.get_fieldsets(request)
43 43
 [(None, {'fields': ['name', 'bio']})]
44  
->>> ma.fieldsets_change(request, band)
  44
+>>> ma.get_fieldsets(request, band)
45 45
 [(None, {'fields': ['name', 'bio']})]
46 46
 
47 47
 
@@ -52,9 +52,9 @@ class and an AdminSite instance, so let's just go ahead and do that manually
52 52
 ...     fields = ['name']
53 53
 
54 54
 >>> ma = BandAdmin(Band, site)
55  
->>> ma.fieldsets_add(request)
  55
+>>> ma.get_fieldsets(request)
56 56
 [(None, {'fields': ['name']})]
57  
->>> ma.fieldsets_change(request, band)
  57
+>>> ma.get_fieldsets(request, band)
58 58
 [(None, {'fields': ['name']})]
59 59
 
60 60
 
@@ -70,18 +70,18 @@ class and an AdminSite instance, so let's just go ahead and do that manually
70 70
 ...     fields = ['name']
71 71
 
72 72
 >>> ma = BandAdmin(Band, site)
73  
->>> ma.form_add(request).base_fields.keys()
  73
+>>> ma.get_form(request).base_fields.keys()
74 74
 ['name']
75  
->>> ma.form_change(request, band).base_fields.keys()
  75
+>>> ma.get_form(request, band).base_fields.keys()
76 76
 ['name']
77 77
 
78 78
 >>> class BandAdmin(ModelAdmin):
79 79
 ...     fieldsets = [(None, {'fields': ['name']})]
80 80
 
81 81
 >>> ma = BandAdmin(Band, site)
82  
->>> ma.form_add(request).base_fields.keys()
  82
+>>> ma.get_form(request).base_fields.keys()
83 83
 ['name']
84  
->>> ma.form_change(request, band).base_fields.keys()
  84
+>>> ma.get_form(request, band).base_fields.keys()
85 85
 ['name']
86 86
 
87 87
 

0 notes on commit c323623

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