Skip to content

Commit

Permalink
Better documentation with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
paulocheque committed Jul 10, 2015
1 parent 48d2567 commit a2ad74c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
56 changes: 50 additions & 6 deletions docs/source/features.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.. _more:

Configuration
Features and Configurations
*******************************************************************************

https://docs.djangoproject.com/en/dev/ref/contrib/admin/
Expand All @@ -9,12 +9,17 @@ https://docs.djangoproject.com/en/dev/ref/contrib/admin/
Registering your models
===============================================================================

Do not use the standard Django method to register your application::
**Do not use** the standard Django method to register your application::

from django.contrib import admin
admin.site.register(YourModel, YourModelAdmin)

Rather that, register your models using the DSA methods, by *model* or *app* that it is explained in the following sections.
Rather that, register your models using one of the following functions, by *model* or *app* that it is explained in the following sections:

- **auto_configure_admin_for_model(model, override=False, **kwargs)**
- **auto_configure_admin_for_app(app, override=False)**
- **auto_configure_admin(applications=[], exclude_applications=[], override=False)**



Configuration per model
Expand Down Expand Up @@ -65,17 +70,56 @@ And to exclude some application::
Settings
===============================================================================


DSA_FIELD_STRATEGY
-------------------

Sometimes you can to override the default intelligence of the tool. To do that, define the **DSA_FIELD_STRATEGY** settings in your ``settings.py`` file::

# Functions that receive a field instance and return a boolean
# Functions that receive a field instance (e.g: `models.CharField(max_length=1)`) and return a boolean
# If this functions returns True, the field will be included in the admin attribute config (e.g: `Admin.raw_id_fields`)
DSA_FIELD_STRATEGY = {
'raw_id_fields': lambda field: True,
}

**For example**, lets create a strategy that we will configure the `list_filter` for every model field that contains `choices`::

# This module offers a set of useful introspection functions to manipulate Django models/fields
from django_smart_autoregister.django_helper import field_has_choices

class MyModel(models.Model):
my_choices = models.CharField(max_length=2, choices=(('A', 'A'), ('B', 'B')))
another_choices = models.CharField(max_length=2)

DSA_FIELD_STRATEGY = {
'list_filter': lambda field: field_has_choices(field),
}

# `Admin.list_filter` will return `['my_choices']`

Or we can customize some default values::

DSA_FIELD_STRATEGY = {
'list_per_page': 20,
'list_max_show_all': 50,
}


DSA_FULL_STRATEGY
-------------------

Sometimes you can put some intelligence after the value was created by the tool or by the **DSA_FIELD_STRATEGY** settings. To to that define a strategy that will receive the generated value and fixed it according to your ideas throw the settings **DSA_FULL_STRATEGY**::
Sometimes you can put some intelligence after the value (e.g: `list_field = [field1, field2, field7]`) was created by the tool or by the **DSA_FIELD_STRATEGY** settings. To to that define a strategy that will receive the generated value and fixed it according to your ideas throw the settings **DSA_FULL_STRATEGY**::

# function(values) => values
DSA_FULL_STRATEGY = {
'raw_id_fields': lambda field: True,
'raw_id_fields': lambda values: values_updated,
}

**For example**, we want to show in the maximum 5 columns in the admin list table::

DSA_FULL_STRATEGY = {
'list_display': lambda values: values[0:5],
}

# Without this config: Admin.list_display = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# With this last config: Admin.list_display = ['a', 'b', 'c', 'd', 'e']
6 changes: 3 additions & 3 deletions docs/source/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ Upgrade
Compatibility
===============================================================================

* Tested with Django 1.2, 1.3, 1.4, 1.5, 1.6, 1.7 and PyPy
* Tested with Python 2.7, 3.3 and 3.4
* Tested with Django 1.4, 1.5, 1.6, 1.7, 1.8
* Tested with Python 2.7, 3.3, 3.4 and PyPy


Motivation
Expand All @@ -84,4 +84,4 @@ It is boring to configure Django admin application for every model. It is a repl
External references
===============================================================================

*
* TODO

0 comments on commit a2ad74c

Please sign in to comment.