Skip to content

Commit

Permalink
Fixed #92 -- meta.Admin 'fields' parameter is now optional. If it's n…
Browse files Browse the repository at this point in the history
…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
adrianholovaty committed Jul 21, 2005
1 parent 304b08e commit c21f6ec
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 41 deletions.
15 changes: 11 additions & 4 deletions django/core/meta.py
Expand Up @@ -2136,7 +2136,7 @@ def __init__(self, to, name, field_name, num_in_admin=0, edit_inline=False,
self.raw_id_admin = raw_id_admin

class Admin:
def __init__(self, fields, js=None, list_display=None, list_filter=None, date_hierarchy=None,
def __init__(self, fields=None, js=None, list_display=None, list_filter=None, date_hierarchy=None,
save_as=False, ordering=None, search_fields=None, save_on_top=False):
self.fields = fields
self.js = js or []
Expand All @@ -2148,10 +2148,17 @@ def __init__(self, fields, js=None, list_display=None, list_filter=None, date_hi
self.save_on_top = save_on_top

def get_field_objs(self, opts):
# Returns self.fields, except with fields as Field objects instead of
# field names.
"""
Returns self.fields, except with fields as Field objects instead of
field names. If self.fields is None, defaults to putting every
non-AutoField field with editable=True in a single fieldset.
"""
if self.fields is None:
field_struct = ((None, {'fields': [f.name for f in opts.fields + opts.many_to_many if f.editable and not isinstance(f, AutoField)]}),)
else:
field_struct = self.fields
new_fieldset_list = []
for fieldset in self.fields:
for fieldset in field_struct:
new_fieldset = [fieldset[0], {}]
new_fieldset[1].update(fieldset[1])
admin_fields = []
Expand Down
3 changes: 0 additions & 3 deletions django/models/auth.py
Expand Up @@ -20,9 +20,6 @@ class Group(meta.Model):
)
ordering = (('name', 'ASC'),)
admin = meta.Admin(
fields = (
(None, {'fields': ('name', 'permissions')}),
),
search_fields = ('name',),
)

Expand Down
3 changes: 0 additions & 3 deletions django/models/core.py
Expand Up @@ -65,9 +65,6 @@ class Redirect(meta.Model):
unique_together=(('site_id', 'old_path'),)
ordering = (('old_path', 'ASC'),)
admin = meta.Admin(
fields = (
(None, {'fields': ('site_id', 'old_path', 'new_path')}),
),
list_display = ('__repr__',),
list_filter = ('site_id',),
search_fields = ('old_path', 'new_path'),
Expand Down
11 changes: 8 additions & 3 deletions docs/model-api.txt
Expand Up @@ -304,9 +304,9 @@ Field Types
many toppings on a pizza)::

meta.ForeignKey(Pizza)

.. admonition:: Note

To create a recursive relationship, use a ``ForeignKey`` that relates
to ``"self"`` (i.e. ``meta.ForeignKey("self")``).

Expand Down Expand Up @@ -568,7 +568,7 @@ Admin options

The ``admin`` field in the model tells Django how to construct the admin
interface for the object. The field is an instance of the ``meta.Admin``
object, which has the following options (of which only ``fields`` is required):
object, which has the following options. All are optional.

``date_hierarchy``
To allow filtering of objects in the admin by date, set ``date_hierarchy``
Expand Down Expand Up @@ -616,6 +616,11 @@ object, which has the following options (of which only ``fields`` is required):

.. image:: http://media.djangoproject.com/img/doc/flatfiles_admin.png

If ``fields`` isn't given but a model does define ``admin`` as a
``meta.Admin`` object, Django will default to displaying each field that
isn't an ``AutoField`` and has ``editable=True``, in a single fieldset, in
the same order as the ``fields`` in the model.

``js``
A list of strings representing URLs of JavaScript files to link into the
admin screen. This can be used to tweak a given type of admin page in JS or
Expand Down
10 changes: 1 addition & 9 deletions docs/overview.txt
Expand Up @@ -138,15 +138,7 @@ classes::
meta.TextField('article'),
meta.ForeignKey(Reporter),
)
admin = meta.Admin(
fields = (
(None, {'fields': ('headline', 'article')}),
('Extra stuff', {'fields': ('pub_date', 'reporter_id')}),
),
)

The ``admin.fields`` defines the layout of your admin form. Each element in the
fields tuple corresponds to a ``<fieldset>`` in the form.
admin = meta.Admin()

The philosophy here is that your site is edited by a staff, or a client, or
maybe just you -- and you don't want to have to deal with creating backend
Expand Down
23 changes: 4 additions & 19 deletions docs/tutorial02.txt
Expand Up @@ -92,11 +92,7 @@ file and make the following change to add an ``admin`` attribute::
fields = (
# ...
)
admin = meta.Admin(
fields = (
(None, {'fields': ('question', 'pub_date')}),
),
)
admin = meta.Admin()

Restart your development Web server, and reload the Django admin page. You'll
have to restart the server each time you make a change to Python code -- but
Expand Down Expand Up @@ -163,8 +159,8 @@ Customize the admin form

Take a few minutes to marvel at all the code you didn't have to write.

Let's customize this a bit. We can reorder the fields by changing the
order of the field names in the ``admin`` attribute of the model::
Let's customize this a bit. We can reorder the fields by explicitly adding a
``fields`` parameter to ``meta.Admin``::

admin = meta.Admin(
fields = (
Expand Down Expand Up @@ -226,15 +222,7 @@ Here's what that would look like::

class Choice(meta.Model):
# ...
admin = meta.Admin(
fields = (
(None, {'fields': ('poll_id', 'choice', 'votes')}),
),
)

(Note that we used "poll_id" to refer to the ``ForeignKey(Poll)`` field. The
field name is automatically calculated from the model's class name, lowercased,
plus '_id'.)
admin = meta.Admin()

Now "Choices" is an available option in the Django admin. The "Add choice" form
looks like this:
Expand Down Expand Up @@ -318,9 +306,6 @@ on the change list page for the object::
class Poll(meta.Model):
# ...
admin = meta.Admin(
fields = (
(None, {'fields': ('question', 'pub_date')}),
),
list_display = ('question', 'pub_date'),
)

Expand Down

0 comments on commit c21f6ec

Please sign in to comment.