• Stars
    star
    158
  • Rank 229,512 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created about 13 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

(formerly known as django-salmonella) A raw_id_fields widget replacement that handles display of an object's string value on change and can be overridden via a template.

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

Compatibility Matrix:

Py/Dj 3.7 3.8 3.9 3.10
3.2 βœ“ βœ“ βœ“ βœ“
4.0 βœ“ βœ“ βœ“ βœ“

Rationale

By default, Django’s admin loads all possible related instances into a select-box interface (<select>) for fields that are ForeignKey. This can result in long load times and unresponsive admin pages for models with thousands of instances, or with multiple ForeinKeys.

The normal fix is to use Django's ModelAdmin.raw_id_fields, but by default it only shows the raw id of the related model instance, which is somewhat unhelpful.

This package improve the user experience by providing the string representation or other customized text for the related instance, linked to that instance's admin change form, in addition to the raw id itself.

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. Make sure its listed before the generic admin.site.urls urlpattern include:

urlpatterns = [
    # ...
    path('admin/dynamic_raw_id/', include('dynamic_raw_id.urls')),
    path("admin/", admin.site.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 pytest

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 pytest

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

Note

The default settings file is set in the .env file which pipenv automatically exposes:

DJANGO_SETTINGS_MODULE=dynamic_raw_id.tests.testapp.settings

More Repositories

1

python-qrcode

Python QR Code image generator
Python
4,060
star
2

salmon

A simple monitoring system.
Python
478
star
3

amygdala

RESTful HTTP client for JavaScript powered web applications
JavaScript
392
star
4

django-layout

Django project template layout
Python
272
star
5

django-startproject

Boilerplate code for new Django projects.
Python
238
star
6

goodconf

Transparently load variables from environment or JSON/YAML/TOML file.
Python
123
star
7

django-debug-logging

A plugin for the Django-Debug-Toolbar to provide statistic logging and a UI for reviewing the logs.
Python
103
star
8

django-webserver

Production webservers as a Django management command
Python
86
star
9

django-production

production settings for Django
Python
77
star
10

generator-frigate

Yeoman generator for webapp development with React, webpack hot reloading, es6 (babel), sass, karma and BrowserSync as a development server/proxy.
JavaScript
44
star
11

django-geotagging

Generic geotagging app built on GeoDjango
Python
43
star
12

lincoln-loop-deploy

An easy-to-use Fabric script for deploying Python projects
Python
41
star
13

django-cpserver

Management commands for serving Django via CherryPy's built-in WSGI server
Python
41
star
14

salt-stats

A collection of modules for collecting stats
Python
37
star
15

django-jsonit

Helpers for working with JSON responses in your Django project.
Python
35
star
16

django-alive

Healthchecks for Django
Python
32
star
17

django-protected-files

A Django application that lets you serve protected static files via your frontend server after authorizing the user against django.contrib.auth.
Python
32
star
18

round-up

Links and notes from the Django Round-Up podcast
30
star
19

emailed-me

A mini-site for checking Google's Gmail feed with Oauth.
Python
29
star
20

python-package-manager-shootout

Benchmarking various Python package managers
HTML
27
star
21

django-beancounter

Django-beancounter is a simple app I built to track my income and expenses.
JavaScript
26
star
22

django-ft-cache

A fault-tolerant pylibmc cache backend for Django
Python
24
star
23

django-render

Render unknown Django model instances based on their content type.
Python
21
star
24

saltdash

A read-only dashboard for Salt jobs
Python
21
star
25

django-kwalitee

A suite of scripts to measure the "kwalitee" of a Django project.
Python
21
star
26

django-selenium-intro

Introduction to Django Selenium blog post example
Python
20
star
27

crossing

JavaScript URL utility library
JavaScript
17
star
28

jquery.shuffleLetters

A fork of martinaglv's shuffleLetters discussed here: http://tutorialzine.com/2011/09/shuffle-letters-effect-jquery/
JavaScript
16
star
29

django-mailfriend

Generic "mail this to a friend" app for Django
Python
13
star
30

django-mineral

A collection of templates/widgets for rapid prototyping
JavaScript
13
star
31

django-launchpad

A simple application to track mailing list signups and unsubscribes. Useful for pre-launch signup pages.
Python
11
star
32

pyuwsgi-wheels

Wheel builder for pyuwsgi
Python
8
star
33

fab-pave

A Fabric script for paving a single web/database server
Python
8
star
34

django-redmine

A Django application to connect to an existing Redmine database
Python
8
star
35

django-locations

International location app following ISO 3166 standards
Python
7
star
36

django-pyuwsgi

Use django-webserver instead
Python
7
star
37

learn-backbone-integration

Syncing Backbone.js with Django
JavaScript
7
star
38

memcache-top

Automatically exported from code.google.com/p/memcache-top
Perl
5
star
39

python-realtimemagic

WorkInProgress. A set of tools to make writting a realtime sockjs server easy(er)
Java
5
star
40

django-paginav

A Django template pagination navigation tag
Python
4
star
41

paaws

CLI for Paaws
Python
4
star
42

pkg-uwsgi-python

uWSGI for Python Debian/Ubuntu package builder
4
star
43

django-libsql

libsql database backend for Django
Python
3
star
44

Colloquy-MenuBar-Notifier-Plugin

An unobtrusive notifier for Colloquy.
Python
3
star
45

django-smatic

Python
3
star
46

jquery-slidify

A graphical range selector for jQuery
JavaScript
3
star
47

django-safe-admin-emails

Provides support for making sure sensitive information does not appear in admin emails.
3
star
48

external-api-integration

Post subversion commits as Basecamp comments
Python
3
star
49

ecs-task

Helper for registering new task definitions on AWS ECS and updating associated services.
Python
3
star
50

flexmap

A HTML/CSS based sitemap
CSS
2
star
51

threads

Threads, the front-end
JavaScript
2
star
52

docker-graphite-api-grafana

JavaScript
2
star
53

element-queries-example

A news website layout with an article component that uses element queries.
HTML
2
star
54

django-linkback

An admin widget to show a link back to the original objects in foreign keys.
Python
2
star
55

terraform-aws-ecs-service-update

Terraform module to update Services and run Tasks on Amazon ECS
HCL
1
star
56

crossing-react-router-demo

A demo showing using Crossing to manage URLs in a react-router based application
JavaScript
1
star
57

docker-google-auth-proxy

1
star
58

django-geotagging-new

An application for transparently geotagging your models.
Python
1
star
59

vectorprime

Lincoln Loop's hubot
CoffeeScript
1
star
60

django-static-installer

A static file dependency downloader/manager
Python
1
star
61

flexbox-fridays

The website for Lincoln Loop's Flexbox Fridays research project.
CSS
1
star
62

docker-nginx-push-stream

Build Nginx push stream module packages for Ubuntu 14.04
1
star
63

itinerary

Lightweight routing forked from Backbone, decoupled, and (soon to be) enhanced.
JavaScript
1
star
64

web-design-regularly

Landing page for LL's web design weekly
CSS
1
star
65

blinken-bloop

For Django Dash 2013.
CSS
1
star
66

windmills

Stop Tilting at Windmills - Spotting Bottlenecks
Python
1
star
67

docker-sentry

Python
1
star