Coder Social home page Coder Social logo

drlior / django-salmonella Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lincolnloop/django-dynamic-raw-id

0.0 2.0 0.0 115 KB

A raw_id_fields widget replacement that handles display of an object's string value on change and can be overridden via a template.

License: MIT License

JavaScript 15.14% Python 78.47% HTML 6.39%

django-salmonella's Introduction

https://travis-ci.org/lincolnloop/django-salmonella.svg?branch=master https://codecov.io/github/lincolnloop/django-salmonella/coverage.svg?branch=master

django-salmonella

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

Install the package with pip:

$ pip install django-salmonella

Put salmonella to your list of INSTALLED_APPS:

INSTALLED_APPS = (
    # ... other apps
    'salmonella',
)

And add the urlpattern:

urlpatterns = [
    # ...
    url(r'^admin/salmonella/', include('salmonella.urls')),
]

salmonella comes with some static files so don't forget to run manage.py collectstatic.

Usage

To start using django-salmonella in your application all you need to do is implement SalmonellaMixin in your ModelAdmin class and add the desired fields to a list of salmonella_fields:

from salmonella.admin import SalmonellaMixin

class UserProfileAdmin(SalmonellaMixin, admin.ModelAdmin):
    salmonella_fields = ('user',)

You can use Salmonella widgets in a Admin filter as well:

from salmonella.admin import SalmonellaMixin
from salmonella.filters import SalmonellaFilter

class UserProfileAdmin(SalmonellaMixin, admin.ModelAdmin):
   list_filter = (
       ('salmonella_fk', SalmonellaFilter),
   )

Customizing the value of the dynamic widget

The coolest feature of django-salmonella is the ability to customize the output of the value displayed alongside the SalmonellaIdWidget. 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-salmonella looks for this template structure salmonella/<app>/<model>.html and salmonella/<app>/multi_<model>.html (for multi-value lookups).

For instance, if I have a blog post with a User salmonella field that I want display as Firstname Lastname, I would create the template salmonella/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 Salmonella scripts located on a different prefix than /admin/salmonella/ you need adjust the SALMONELLA_MOUNT_URL JS variable.

Example:

# In case the script is setup at /foobar/salmonella/
url(r'^foobar/salmonella/', include('salmonella.urls')),

# Provide a
<script>
    window.SALMONELLA_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.SALMONELLA_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 %}

django-salmonella's People

Contributors

barttc avatar sethtrain avatar yml avatar gustavo-sdo avatar kmike avatar dxist avatar morlandi avatar scdoshi avatar sk1p avatar dpnova avatar nburlett avatar skevy avatar jeremyjbowers avatar mlouro avatar bkonkle avatar hstanev avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.