raw_id_fields widget replacement that handles display of an object's string value on change and can be overridden via a template.
Project description
django-dynamic-raw-id
A Django admin raw_id_fields widget replacement that handles display of an object’s string value on change and can be overridden via a template. See this example:
Installation
Install the package with pip:
$ pip install django-dynamic-raw-id
Put dynamic_raw_id to your list of INSTALLED_APPS:
INSTALLED_APPS = (
# ... other apps
'dynamic_raw_id',
)
And add the urlpattern:
urlpatterns = [
# ...
url(r'^admin/dynamic_raw_id/', include('dynamic_raw_id.urls')),
]
dynamic_raw_id comes with some static files so don’t forget to run manage.py collectstatic.
Usage
To start using django-dynamic-raw-id in your application all you need to do is implement DynamicRawIDMixin in your ModelAdmin class and add the desired fields to a list of dynamic_raw_id_fields:
from dynamic_raw_id.admin import DynamicRawIDMixin
class UserProfileAdmin(DynamicRawIDMixin, admin.ModelAdmin):
dynamic_raw_id_fields = ('user',)
You can use dynamic_raw_id widgets in a Admin filter as well:
from dynamic_raw_id.admin import DynamicRawIDMixin
from dynamic_raw_id.filters import DynamicRawIDFilter
class UserProfileAdmin(DynamicRawIDMixin, admin.ModelAdmin):
list_filter = (
('dynamic_raw_id_fk', DynamicRawIDFilter),
)
Customizing the value of the dynamic widget
The coolest feature of django-dynamic-raw-id is the ability to customize the output of the value displayed alongside the DynamicRawIDWidget. There is a basic implementation if all you want is your object’s __unicode__ value. To change the value displayed all you need to do is implement the correct template.
django-dynamic-raw-id looks for this template structure dynamic_raw_id/<app>/<model>.html and dynamic_raw_id/<app>/multi_<model>.html (for multi-value lookups).
For instance, if I have a blog post with a User dynamic_raw_id field that I want display as Firstname Lastname, I would create the template dynamic_raw_id/auth/user.html with:
<span>{{ object.0.first_name }} {{ object.0.last_name }}</span>
A custom admin URL prefix
If you have your admin and the dynamic_raw_id scripts located on a different prefix than /admin/dynamic_raw_id/ you need adjust the DYNAMIC_RAW_ID_MOUNT_URL JS variable.
Example:
# In case the app is setup at /foobar/dynamic_raw_id/
url(r'^foobar/dynamic_raw_id/', include('dynamic_raw_id.urls')),
# Provide a
<script>
window.DYNAMIC_RAW_ID_MOUNT_URL = "{% url "admin:index" %}";
</script>
An ideal place is the admin base_site.html template. Full example:
{% extends "admin/base.html" %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block extrahead %}
{{ block.super }}
<script>
window.DYNAMIC_RAW_ID_MOUNT_URL = "{% url "admin:index" %}";
</script>
{% endblock %}
{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('Django administration') }}</a></h1>
{% endblock %}
{% block nav-global %}{% endblock %}
Testing and Local Development
The testsuite uses Selenium to do frontend tests, we require Firefox and geckodriver to be installed. You can install geckodriver on OS X with Homebrew:
$ brew install geckodriver
Run the testsuite in your local environment using:
$ cd django-dynamic-raw-id/
$ pipenv install --dev
$ pipenv run python ./runtests.py
Or use tox to test against various Django and Python versions:
$ tox -r
You can also invoke the test suite or other ‘manage.py’ commands by calling the django-admin tool with the test app settings:
$ cd django-dynamic-raw-id/
$ pipenv install --dev
$ pipenv run django-admin
$ pipenv run django-admin test
This also allows you to run the internal testing app in a testserver, to preview a sample of what django-dynamic-raw-id is doing:
$ pipenv run django-admin migrate
$ pipenv run django-admin createsuperuser
$ pipenv run django-admin runserver
DJANGO_SETTINGS_MODULE=dynamic_raw_id.tests.testapp.settings
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-dynamic-raw-id-2.4.tar.gz
.
File metadata
- Download URL: django-dynamic-raw-id-2.4.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3223d627c30628a5e9c05dc24dfa4e1ed90b8dab99fa311fe33e83ad3d4f97b |
|
MD5 | 3a65eeb560e938f517c2a317f6147c7c |
|
BLAKE2b-256 | c538e564d75851a4e6acb6bb01d45b02b5cffd248cf745e065b0c61c26ee7693 |
File details
Details for the file django_dynamic_raw_id-2.4-py2.py3-none-any.whl
.
File metadata
- Download URL: django_dynamic_raw_id-2.4-py2.py3-none-any.whl
- Upload date:
- Size: 23.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cc0d08bee5932c09af204456c31d973c3a21a0f56bc96ae3eabf2c82f81503d |
|
MD5 | a4f9d654a1ab447f916c7a4c6b6034bf |
|
BLAKE2b-256 | ce1e4ae947065d64cb60a6e46dc053da33bd9dda88985998c7ef87ebce9b18ee |