Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (testpypi.python.org).
Help us improve Python packaging - Donate today!

Quickly create admin related actions, list properties and single instance properties for Django

Project Description
#Django admin methods
Easily create [admin actions](https://docs.djangoproject.com/en/1.7/ref/contrib/admin/actions/) methods, [list field methods](https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display) and [model methods](https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fields) for Django ModelAdmin
##Common usage
Use to quickly create Django admin items such as:

- *"Set as published"*, *"Set as unpublished"* actions in the admin list view
- Shortened descpription (limit to *n* characters) for use in the admin list view
- Image thumbnail in change view

## API

###Actions

The *actions* module provides methods to build [admin actions](https://docs.djangoproject.com/en/1.7/ref/contrib/admin/actions/) and should be used within a **admin.ModelAdmin** declaration

#### true_false(field_name, true_name='', false_name='')

Returns two functions, one to set the model's *field_name* as true, one as false.
If *true_name* is given it will be used as the *short_description* value on the "true" function (which will be shown in the admin actions list).
If no name is given, defaults to *"Set as [field_name]"* and *"Set as non-[field_name]"

##### Sample code

```python
import admin_methods.actions

class PropertyAdmin(admin.ModelAdmin):
set_published, set_unpublished = admin_methods.actions.true_false('published')
set_highlighted, set_unhighlighted = admin_methods.actions.true_false('highlighted')
actions = [set_published, set_unpublished, set_highlighted, set_unhighlighted]
```

Results in the actions below which would set the selected items' *published* value to true or false

![admin actions](https://cloud.githubusercontent.com/assets/487758/6201646/5da29110-b4f0-11e4-9b28-645906e4d2e0.png)

#### toggle(field_name, name='')
Returns a function which sets the model's *field_name* as the opposite of its current value for each selected items.
If *name* is given it will be used as the *short_description* value on the function (which will be shown in the admin actions list).
If no name is given, defaults to *"Toggle [field_name]"*

### List

The *list* module provides methods to create extra fields for use in the list view and should be used within a **admin.ModelAdmin** declaration

#### short_text(field_name, length=200, name='', suffix='...', strip_html=False)
Returns a function to be used as a *list_display* entry.
If no name is given, *field_name* is used.
You may change the suffix which will be appended to **shortened** text only.
Use *strip_html* to remove HTML tags **before** length calculation

```python
import admin_methods.list

class PropertyAdmin(admin.ModelAdmin):
list_display = ('short_description',)
short_description = admin_methods.list.short_text('description', length=150, strip_html=True)
```

Description will be shortened to 150 characters where necessary:
![shortened text](https://cloud.githubusercontent.com/assets/487758/6201671/27c5ff4e-b4f2-11e4-878c-1c258f50f44c.png)

*Strip html* transforms this
![nostrip](https://cloud.githubusercontent.com/assets/487758/6201670/27c4228c-b4f2-11e4-9611-b2696c3fd66e.png)

into this
![stripped](https://cloud.githubusercontent.com/assets/487758/6201672/27c76794-b4f2-11e4-93d7-96a576285604.png)

### Model
The *model* module provides methods to create extra fields for use in the add or change view and should be used within a **models.Model** declaration

#### image_thumb(field_name, name='', width=100, description=False, if_no_image='')

Returns a function to be set on a model, which can then be used in the *fields* and *readonly_fields* declaration of a ModelAdmin

#####Parameters

| Parameter | Default | Description |
|-------------|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| field_name | | the model ImageField name |
| name | '' | the name for the function. Will be set to the field_name if not provided. <br>**Note** Django needs a name to detect unique methods, but you can make sure that it does not get displayed on the page by setting description to False (default) |
| width | 100 | image width |
| description | False | set *name* as short_description (and hence show it on the page) or not |
| if_no_image | '' | value to display if no image is found |

##### Sample code

**models.py**
```python
class Property(django.db.models.Model):
top_image = django.db.models.ImageField(max_length=255, upload_to='properties/top')
# Admin fields
top_image_preview = admin_actions.model.image_thumb('top_image')
```

**admin.py**
```python
class PropertyAdmin(admin.ModelAdmin):
fields = ('top_image', 'top_image_preview',)
readonly_fields = ('top_image_preview',)
```

**Result**
![image thumb](https://cloud.githubusercontent.com/assets/487758/6201736/b9a489e6-b4f5-11e4-8ac3-da43a75fe5d1.png)

## Dependencies

- Uses [html2text](https://github.com/aaronsw/html2text) for stripping html in *list.short_text*

## Releases

###0.1.2

Changelog

- Improved README

###0.1.1

Changelog

- Improved README

###0.1.0

Initial release

## TODO

- Testing!
Release History

Release History

This version
History Node

0.1.2

History Node

0.1.1

History Node

0.1.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django_admin_methods-0.1.2-py2.py3-none-any.whl (8.7 kB) Copy SHA256 Checksum SHA256 py2.py3 Wheel Feb 15, 2015
django-admin-methods-0.1.2.tar.gz (5.5 kB) Copy SHA256 Checksum SHA256 Source Feb 15, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting