Skip to main content

raw_id_fields widget replacement that handles display of an object's string value on change.

Project description

https://img.shields.io/pypi/v/django-dynamic-raw-id.svg https://travis-ci.org/bartTC/django-dynamic-raw-id.svg?branch=master https://api.codacy.com/project/badge/Coverage/bb93482e6a6348058b993a42951a9f19 https://api.codacy.com/project/badge/Grade/bb93482e6a6348058b993a42951a9f19

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:

http://d.pr/i/10GtM.png

Installation

The app is compatible and tested with Python 2.7 → 3.7 and all versions of Django between 1.8 → 2.1.

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 test

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

Changelog

v2.6 (2019-06-21)

  • BACKWARDS INCOMPATIBLE: Dropped support for Django <1.11.

  • BACKWARDS INCOMPATIBLE: Dropped support for Python 3.4.

  • Django 2.2 compatibility and tests.

  • General code cleanup.

  • Pipenv support for local development.

  • Some visual fixes around icons and alignment.

v2.5 (2018-12-09)

  • Django 2.1 compatibility and tests.

v2.4 (2018-04-09)

  • Fixes missing icons in Admin views.

  • Fixes missing JS handling when using a custom /admin/ url.

v2.3 (2018-01-18)

  • BACKWARDS INCOMPATIBLE: Renamed the project to django-dynamic-raw-id. to reflect what it’s actually doing.

  • Fixed glass lookup icon in Django 1.10 and below.

  • Specific ordering of media asset loading.

v1.2 (2018-01-17)

  • Multiple fixes and enhancements.

  • Full Selenium based testsuite.

  • Django 2.0 and Python 3 compatibility.

  • pipenv support.

Project details


Download files

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

Source Distribution

django-dynamic-raw-id-2.6.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

django_dynamic_raw_id-2.6-py2.py3-none-any.whl (36.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-dynamic-raw-id-2.6.tar.gz.

File metadata

  • Download URL: django-dynamic-raw-id-2.6.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for django-dynamic-raw-id-2.6.tar.gz
Algorithm Hash digest
SHA256 3f449e682a75739b96cdeef224e9b720a4e0f44468f39c79ea829a277607f388
MD5 0ab8e548958711c753bfe7e67e842d37
BLAKE2b-256 2651355b493da534501ac54096a69597a9dbe64b91380d6f4e8ef9c5b456d4e2

See more details on using hashes here.

File details

Details for the file django_dynamic_raw_id-2.6-py2.py3-none-any.whl.

File metadata

  • Download URL: django_dynamic_raw_id-2.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 36.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for django_dynamic_raw_id-2.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 af9cbc6f8744a64d037483b644e3c43497320ebd616a31eea0f950098d814384
MD5 c9a0eb6567c84091233257646cbd7bf6
BLAKE2b-256 0f211e4f47e6d5545d8bf70106ea672859bae7faaf19e7c27d8b4331916f07c6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page