Allow foreign key attributes in list_display with '__'

Overview

django-related-admin

https://travis-ci.org/PetrDlouhy/django-related-admin.svg?branch=master https://coveralls.io/repos/github/PetrDlouhy/django-related-admin/badge.svg?branch=master

Allow foreign key attributes in Django admin change list list_display with '__'

This is based on DjangoSnippet 2996 which was made by Kpacn.

Installation

  1. This library is on PyPI so you can install it with:

    pip install django-related-admin
    

or from github:

pip install git+https://github.com/PetrDlouhy/django-related-admin#egg=django-related-admin
  1. Add "related_admin" to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = (
        ...
        'related_admin',
        'django.contrib.admin',
    )
    

Note: Django-related-admin changes change_list.html template to disable white-space: nowrap; css class on admin headers to allow long (posibly related) header names to wrap. If you want this behaviour, add the app before django.contrib.admin, otherwise after.

Usage

Just use it instead of model.Admin:

from related_admin import RelatedFieldAdmin
from related_admin import getter_for_related_field

class FooAdmin(RelatedFieldAdmin):
    # these fields will work automatically (and boolean fields will display an icon):
    list_display = ('address__phone','address__country__country_code','address__foo')

    # ... but you can also define them manually if you need to override short_description or boolean parameter:
    address__foo = getter_for_related_field('address__foo', short_description='Custom Name', boolean=True)
Comments
  • Automatic detection of boolean fields

    Automatic detection of boolean fields

    Adds support for automatically detecting boolean fields, so you can get selection_014

    Instead of selection_015

    By only listing the field in list_display (like vanilla Django)

    class AlbumAdmin(RelatedFieldAdmin):
        list_display = ('artist__active',)
    

    And without requiring an explicit boolean=True:

    class AlbumAdmin(RelatedFieldAdmin):
        list_display = ('artist__active',)
    
        # Not needed
        artist__active = getter_for_related_field('artist__active', boolean=True)
    
    opened by theY4Kman 24
  • What is the license for django-related-admin?

    What is the license for django-related-admin?

    I could not find a definite answer for this ... there isn't a license mentioned in the README and no LICENSE file at the root. Source files don't mention a license either (as far as I could tell.).

    Could somebody authoritatively state which license this project is distributed under?

    thank you!

    opened by aboisvert 11
  • explicitly add self.list_select_related to select_related

    explicitly add self.list_select_related to select_related

    Added code to explicitly add contents of self.list_select_related to select_related when creating a queryset. This can be useful when list_display contains a method name a that method uses a foreign key not otherwise listed in list_display.

    opened by nektor211 11
  • Update __init__.py

    Update __init__.py

    Fixed this error, caused by performing string manipulation on a function:

    related_admin/__init__.py", line 69, in __new__
        if '__' in field[1:-1] and not hasattr(new_class, field):
    TypeError: 'function' object is not subscriptable
    

    list_display can also contain functions. Added check that fields in list_display are only strings.

    opened by prakhar-goel 10
  • Use value as first argument

    Use value as first argument

    Choice fields (TextField or CharField with choices argument set) always have their value in the list displayed as empty.

    This fixes it. And doesn't seem to break anything else for me, but please make sure I'm not doing something stupid before merging please!

    FYI, the change was introduced here

    opened by leibowitz 4
  • break list_filter

    break list_filter

    Hi, Thanks for this app, it works like a charm and permitted me to remove lots of boilerplate code of my admin files.

    Nevertheless, I tried to add one of those fields in my list_filter and it seems it does not like that ;-)

    admin.E116) The value of 'list_filter[2]' refers to 'fmt_forecast_steps', which does not refer to a Field

    Is it a known issue and easy fixable?

    TIA

    opened by flamby 2
  • Unable to unregister ModelAdmin

    Unable to unregister ModelAdmin

    If I try to unregister ModelAdmin with RelatedFieldAdmin ancestor I got error:

      File "/home/petr/soubory/programovani/blenderkit/blenderhub_server/blenderhub/apps/history_register/admin.py", line 7, in <module>
        admin.site.unregister(PaymentAdmin)
      File "/home/petr/.cache/pypoetry/virtualenvs/blenderkit-server-eqrfB9dY-py3.10/lib/python3.10/site-packages/django/contrib/admin/sites.py", line 159, in unregister
        for model in model_or_iterable:
    TypeError: 'RelatedFieldAdminMetaclass' object is not iterable
    
    opened by PetrDlouhy 1
  • The code breaks when using methods as fields

    The code breaks when using methods as fields

    The code breaks on valid admins such as these:

    import relatedadmin
    from . import models
    
    @admin.register(models.MyModel)
    class MyModelAdmin(related_admin.RelatedFieldAdmin):
        def get_attribute(self):
            return self.attribute
    
        list_display = [get_attribute]
    

    Error:

      File "related_admin/__init__.py", line 69, in __new__
        if '__' in field[1:-1] and not hasattr(new_class, field):
    TypeError: 'function' object is not subscriptable
    
    opened by wolph 1
  • getter_for_related_field not working in Django 1.9

    getter_for_related_field not working in Django 1.9

    Here's a simplified example:

    class LegalAdmin(RelatedFieldAdmin):
        ordering = ('user__first_name',
                    )
        list_display = ('user__first_name',
                        'legal_name',
                        'user__is_active'
                        )
    
        user__first_name = getter_for_related_field('user__first_name', short_description='Name')
    

    This results in the following (note it's still showing as FIRST NAME although it should be NAME):

    2016-05-28 14_03_26-selecteaza salon pentru schimbare _ administrare site django

    opened by shnako 1
  • Boolean

    Boolean

    Boolean attributes appears as "True" os "False" instead of the "check icon" in Admin. Is it possible to show the "check icon" using the django-related-admin? How? Thanks! (Sorry for the english!)

    opened by ricardobfreitas 1
  • Pypi Wrong Package

    Pypi Wrong Package

    I tried to install this package from pip, but it installed another package. I think it installed this package https://pypi.python.org/pypi/django-related-admin/1.0.0 instead.

    Also, how about add support for generating link(anchor) on the related model, instead of text.

    opened by edwinlunando 1
Owner
Petr Dlouhý
If you like my work, you can support me on Patreon: https://www.patreon.com/PetrDlouhy
Petr Dlouhý
Fully Automated YouTube Channel ▶️with Added Extra Features.

Fully Automated Youtube Channel ▒█▀▀█ █▀▀█ ▀▀█▀▀ ▀▀█▀▀ █░░█ █▀▀▄ █▀▀ █▀▀█ ▒█▀▀▄ █░░█ ░░█░░ ░▒█░░ █░░█ █▀▀▄ █▀▀ █▄▄▀ ▒█▄▄█ ▀▀▀▀ ░░▀░░ ░▒█░░ ░▀▀▀ ▀▀▀░

sam-sepiol 249 Jan 2, 2023
Converts geometry node attributes to built-in attributes

Attribute Converter Simplifies converting attributes created by geometry nodes to built-in attributes like UVs or vertex colors, as a single click ope

Ivan Notaros 12 Dec 22, 2022
Foreign exchange rates, Bitcoin price index and currency conversion using ratesapi.io

forex-python Forex Python is a Free Foreign exchange rates and currency conversion. Note: Install latest forex-python==1.1 to avoid RatesNotAvailableE

MicroPyramid 540 Jan 5, 2023
Find a Doc is a free online resource aimed at helping connect the foreign community in Japan with health services in their native language.

Find a Doc - Localization Find a Doc is a free online resource aimed at helping connect the foreign community in Japan with health services in their n

Our Japan Life 18 Dec 19, 2022
A simple, transparent, open-source key logger, written in Python, for tracking your own key-usage statistics.

A simple, transparent, open-source key logger, written in Python, for tracking your own key-usage statistics, originally intended for keyboard layout optimization.

Ga68 56 Jan 3, 2023
Key Cast - Cast your key presses and mouse clicks on the screen, while casting your favorite application on the screen. Better than the rest.

Key Cast Screen cast your keyboard and mouse clicks in style Project Homepage » View Demo · Report Bug · Request Feature Table of Contents Introductio

Mehul Singh Teya 13 Dec 23, 2022
A program will generate a eth key pair that has the public key that starts with a defined amount of 0

ETHAdressGenerator This short program will generate a eth key pair that has the public key that starts with a defined amount of 0 Requirements Python

null 3 Nov 19, 2021
Key Logger - Key Logger using Python

Key_Logger Key Logger using Python This is the basic Keylogger that i have made

Mudit Sinha 2 Jan 15, 2022
Bleach is an allowed-list-based HTML sanitizing library that escapes or strips markup and attributes

Bleach Bleach is an allowed-list-based HTML sanitizing library that escapes or strips markup and attributes. Bleach can also linkify text safely, appl

Mozilla 2.5k Dec 29, 2022
Tweak the form field rendering in templates, not in python-level form definitions. CSS classes and HTML attributes can be altered.

django-widget-tweaks Tweak the form field rendering in templates, not in python-level form definitions. Altering CSS classes and HTML attributes is su

Jazzband 1.8k Jan 2, 2023
Tweak the form field rendering in templates, not in python-level form definitions. CSS classes and HTML attributes can be altered.

django-widget-tweaks Tweak the form field rendering in templates, not in python-level form definitions. Altering CSS classes and HTML attributes is su

Jazzband 1.8k Jan 6, 2023
Find index entries in $INDEX_ALLOCATION attributes

INDXRipper Find index entries in $INDEX_ALLOCATION attributes Timeline created using mactime.pl on the combined output of INDXRipper and fls. See: sle

null 32 Nov 5, 2022
Fader Networks: Manipulating Images by Sliding Attributes - NIPS 2017

FaderNetworks PyTorch implementation of Fader Networks (NIPS 2017). Fader Networks can generate different realistic versions of images by modifying at

Facebook Research 753 Dec 23, 2022
Bleach is an allowed-list-based HTML sanitizing library that escapes or strips markup and attributes

Bleach Bleach is an allowed-list-based HTML sanitizing library that escapes or strips markup and attributes. Bleach can also linkify text safely, appl

Mozilla 2.5k Dec 29, 2022
PyPI package for scaffolding out code for decision tree models that can learn to find relationships between the attributes of an object.

Decision Tree Writer This package allows you to train a binary classification decision tree on a list of labeled dictionaries or class instances, and

null 2 Apr 23, 2022
This is an example of how to automate Ridit Analysis for a dataset with large amount of questions and many item attributes

This is an example of how to automate Ridit Analysis for a dataset with large amount of questions and many item attributes

Ishan Hegde 1 Nov 17, 2021
dta Convert Dict To Attributes!

dta (Dict to Attributes) dta is very small dict (or json) to attributes converter. It is only have 1 files and applied to every python versions.

Rukchad Wongprayoon 0 Dec 31, 2021
SAN for Product Attributes Prediction

SAN Heterogeneous Star Graph Attention Network for Product Attributes Prediction This repository contains the official PyTorch implementation for ADVI

Xuejiao Zhao 9 Dec 12, 2022
It is a Blender Tool which can convert the Object Data Attributes in face corner to the UVs or Vertex Color.

Blender_ObjectDataAttributesConvertTool It is a Blender Tool which can convert the Object Data Attributes in face corner to the UVs or Vertex Color. D

Takeshi Chō 2 Jan 8, 2022
Two predictive attributes (Speed and Angle) and one attribute target (Power)

Two predictive attributes (Speed and Angle) and one attribute target (Power). A container crane has the function of transporting containers from one point to another point. The difficulty of this task lies in the fact that the container is connected to the bridge crane by cables causing an opening angle while the container is being transported, interfering with the operation at high speeds due to oscillation that occurs at the end point, which could cause accidents.

Astitva Veer Garg 1 Jan 11, 2022