Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #92 -- meta.Admin 'fields' parameter is now optional. If it's n…

…ot given, Django will use all editable fields by default. This cuts down on redundancy. Also updated relevant docs to reflect the change.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@265 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c21f6ecee24c6587bb8ddddc878aad3fd29b40a7 1 parent 304b08e
Adrian Holovaty authored July 21, 2005
15  django/core/meta.py
@@ -2136,7 +2136,7 @@ def __init__(self, to, name, field_name, num_in_admin=0, edit_inline=False,
2136 2136
         self.raw_id_admin = raw_id_admin
2137 2137
 
2138 2138
 class Admin:
2139  
-    def __init__(self, fields, js=None, list_display=None, list_filter=None, date_hierarchy=None,
  2139
+    def __init__(self, fields=None, js=None, list_display=None, list_filter=None, date_hierarchy=None,
2140 2140
         save_as=False, ordering=None, search_fields=None, save_on_top=False):
2141 2141
         self.fields = fields
2142 2142
         self.js = js or []
@@ -2148,10 +2148,17 @@ def __init__(self, fields, js=None, list_display=None, list_filter=None, date_hi
2148 2148
         self.save_on_top = save_on_top
2149 2149
 
2150 2150
     def get_field_objs(self, opts):
2151  
-        # Returns self.fields, except with fields as Field objects instead of
2152  
-        # field names.
  2151
+        """
  2152
+        Returns self.fields, except with fields as Field objects instead of
  2153
+        field names. If self.fields is None, defaults to putting every
  2154
+        non-AutoField field with editable=True in a single fieldset.
  2155
+        """
  2156
+        if self.fields is None:
  2157
+            field_struct = ((None, {'fields': [f.name for f in opts.fields + opts.many_to_many if f.editable and not isinstance(f, AutoField)]}),)
  2158
+        else:
  2159
+            field_struct = self.fields
2153 2160
         new_fieldset_list = []
2154  
-        for fieldset in self.fields:
  2161
+        for fieldset in field_struct:
2155 2162
             new_fieldset = [fieldset[0], {}]
2156 2163
             new_fieldset[1].update(fieldset[1])
2157 2164
             admin_fields = []
3  django/models/auth.py
@@ -20,9 +20,6 @@ class Group(meta.Model):
20 20
     )
21 21
     ordering = (('name', 'ASC'),)
22 22
     admin = meta.Admin(
23  
-        fields = (
24  
-            (None, {'fields': ('name', 'permissions')}),
25  
-        ),
26 23
         search_fields = ('name',),
27 24
     )
28 25
 
3  django/models/core.py
@@ -65,9 +65,6 @@ class Redirect(meta.Model):
65 65
     unique_together=(('site_id', 'old_path'),)
66 66
     ordering = (('old_path', 'ASC'),)
67 67
     admin = meta.Admin(
68  
-        fields = (
69  
-            (None, {'fields': ('site_id', 'old_path', 'new_path')}),
70  
-        ),
71 68
         list_display = ('__repr__',),
72 69
         list_filter = ('site_id',),
73 70
         search_fields = ('old_path', 'new_path'),
11  docs/model-api.txt
@@ -304,9 +304,9 @@ Field Types
304 304
     many toppings on a pizza)::
305 305
 
306 306
         meta.ForeignKey(Pizza)
307  
-        
  307
+
308 308
     .. admonition:: Note
309  
-        
  309
+
310 310
         To create a recursive relationship, use a ``ForeignKey`` that relates
311 311
         to ``"self"`` (i.e. ``meta.ForeignKey("self")``).
312 312
 
@@ -568,7 +568,7 @@ Admin options
568 568
 
569 569
 The ``admin`` field in the model tells Django how to construct the admin
570 570
 interface for the object.  The field is an instance of the ``meta.Admin``
571  
-object, which has the following options (of which only ``fields`` is required):
  571
+object, which has the following options. All are optional.
572 572
 
573 573
 ``date_hierarchy``
574 574
     To allow filtering of objects in the admin by date, set ``date_hierarchy``
@@ -616,6 +616,11 @@ object, which has the following options (of which only ``fields`` is required):
616 616
 
617 617
         .. image:: http://media.djangoproject.com/img/doc/flatfiles_admin.png
618 618
 
  619
+    If ``fields`` isn't given but a model does define ``admin`` as a
  620
+    ``meta.Admin`` object, Django will default to displaying each field that
  621
+    isn't an ``AutoField`` and has ``editable=True``, in a single fieldset, in
  622
+    the same order as the ``fields`` in the model.
  623
+
619 624
 ``js``
620 625
     A list of strings representing URLs of JavaScript files to link into the
621 626
     admin screen. This can be used to tweak a given type of admin page in JS or
10  docs/overview.txt
@@ -138,15 +138,7 @@ classes::
138 138
             meta.TextField('article'),
139 139
             meta.ForeignKey(Reporter),
140 140
         )
141  
-        admin = meta.Admin(
142  
-            fields = (
143  
-                (None, {'fields': ('headline', 'article')}),
144  
-                ('Extra stuff', {'fields': ('pub_date', 'reporter_id')}),
145  
-            ),
146  
-        )
147  
-
148  
-The ``admin.fields`` defines the layout of your admin form. Each element in the
149  
-fields tuple corresponds to a ``<fieldset>`` in the form.
  141
+        admin = meta.Admin()
150 142
 
151 143
 The philosophy here is that your site is edited by a staff, or a client, or
152 144
 maybe just you -- and you don't want to have to deal with creating backend
23  docs/tutorial02.txt
@@ -92,11 +92,7 @@ file and make the following change to add an ``admin`` attribute::
92 92
         fields = (
93 93
             # ...
94 94
         )
95  
-        admin = meta.Admin(
96  
-            fields = (
97  
-                (None, {'fields': ('question', 'pub_date')}),
98  
-            ),
99  
-        )
  95
+        admin = meta.Admin()
100 96
 
101 97
 Restart your development Web server, and reload the Django admin page. You'll
102 98
 have to restart the server each time you make a change to Python code -- but
@@ -163,8 +159,8 @@ Customize the admin form
163 159
 
164 160
 Take a few minutes to marvel at all the code you didn't have to write.
165 161
 
166  
-Let's customize this a bit. We can reorder the fields by changing the
167  
-order of the field names in the ``admin`` attribute of the model::
  162
+Let's customize this a bit. We can reorder the fields by explicitly adding a
  163
+``fields`` parameter to ``meta.Admin``::
168 164
 
169 165
         admin = meta.Admin(
170 166
             fields = (
@@ -226,15 +222,7 @@ Here's what that would look like::
226 222
 
227 223
     class Choice(meta.Model):
228 224
         # ...
229  
-        admin = meta.Admin(
230  
-            fields = (
231  
-                (None, {'fields': ('poll_id', 'choice', 'votes')}),
232  
-            ),
233  
-        )
234  
-
235  
-(Note that we used "poll_id" to refer to the ``ForeignKey(Poll)`` field. The
236  
-field name is automatically calculated from the model's class name, lowercased,
237  
-plus '_id'.)
  225
+        admin = meta.Admin()
238 226
 
239 227
 Now "Choices" is an available option in the Django admin. The "Add choice" form
240 228
 looks like this:
@@ -318,9 +306,6 @@ on the change list page for the object::
318 306
     class Poll(meta.Model):
319 307
         # ...
320 308
         admin = meta.Admin(
321  
-            fields = (
322  
-                (None, {'fields': ('question', 'pub_date')}),
323  
-            ),
324 309
             list_display = ('question', 'pub_date'),
325 310
         )
326 311
 

0 notes on commit c21f6ec

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