Lädt...

🔧 Enhancing Django Admin with Custom Column Display in Django


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

In Django’s admin interface, customizing how model fields are displayed can significantly improve the user experience. One way to achieve this is by defining functions that format model data in a more readable or useful way.

Take, for example, the initialled_name function. It’s designed to display a person’s name in an initialled format, which can be particularly handy when dealing with long lists of names where space is at a premium.

Here’s how it works:

def initialled_name(obj):
    """
    Takes a model instance and returns the person's last name followed by their initials.
    For instance, if obj.first_names='Jerome David' and obj.last_names='Salinger',
    the output would be 'Salinger, J.D.'
    """
    # Extract the first letter of each first name to create the initials
    initials = '.'.join([name[0] for name in obj.first_names.split(' ') if name]) + '.'
    # Format the last name and initials in a string
    return f"{obj.last_names}, {initials}"

To integrate this into the Django admin, we use the list_display attribute of the ModelAdmin class. This attribute specifies which fields should be displayed on the change list page of the admin for a given model.

from django.contrib import admin

class ContributorAdmin(admin.ModelAdmin):
    # Display the initialled name in the admin list view
    list_display = (initialled_name,)

By adding initialled_name to list_display, we tell Django to call our function for each Contributor object in the list page. The function receives the object as its argument and returns the formatted name, which Django then displays in the corresponding column.

This customization not only adds a touch of elegance to the admin interface but also makes it easier to scan through records at a glance.

Source:

Web development with Django, Ben Shaw, Saurabh Badhwar, Chris Guest, Bharath Chandra K S

...

🔧 Enhancing Django Admin with Custom Column Display in Django


📈 61.89 Punkte
🔧 Programmierung

🔧 Sort one column by another column in powerBI


📈 31.73 Punkte
🔧 Programmierung

🪟 Power BI Column Not Found / Can’t Find Column: Quick Fixes


📈 31.73 Punkte
🪟 Windows Tipps

🔧 Vertical alignment of 3 grid-areas in the left column with 1 grid-area in the right column


📈 31.73 Punkte
🔧 Programmierung

🔧 ValueError: A given column is not a column of the dataframe


📈 31.73 Punkte
🔧 Programmierung

🔧 Announcing my new Django package: django-admin-export! #packaging #python #django


📈 31.01 Punkte
🔧 Programmierung

🔧 Enhancing Data Security with Spark: A Guide to Column-Level Encryption - Part 2


📈 25.93 Punkte
🔧 Programmierung

🔧 Enhancing Django Applications with a Custom Middleware


📈 25.77 Punkte
🔧 Programmierung

🔧 Django AllAuth Chapter 5 - Extending Django AllAuth user model with custom fields


📈 23.91 Punkte
🔧 Programmierung

🔧 Using a custom info-column


📈 23.38 Punkte
🔧 Programmierung

🔧 Simplify Your Django Admin with django-unfold


📈 22.81 Punkte
🔧 Programmierung

🕵️ MetInfo 5.3.15 admin/column/delete.php name_2 cross site scripting


📈 22.28 Punkte
🕵️ Sicherheitslücken

🕵️ MetInfo 6.0.0 admin/column/save.php module erweiterte Rechte


📈 22.28 Punkte
🕵️ Sicherheitslücken

🕵️ MetInfo 5.3.15 admin/column/delete.php name_2 Cross Site Scripting


📈 22.28 Punkte
🕵️ Sicherheitslücken

🕵️ Welcart Plugin bis 1.5.2 auf WordPress admin.php search[column]/switch SQL Injection


📈 22.28 Punkte
🕵️ Sicherheitslücken

🕵️ Welcart Plugin bis 1.5.2 auf WordPress admin.php search[column]/switch SQL Injection


📈 22.28 Punkte
🕵️ Sicherheitslücken

🕵️ MetInfo 6.1.3 admin/column/move.php lang_columnerr4 cross site scripting


📈 22.28 Punkte
🕵️ Sicherheitslücken

🕵️ MetInfo 6.0.0 admin/column/save.php module privilege escalation


📈 22.28 Punkte
🕵️ Sicherheitslücken

🔧 How to add authentication to a Django custom admin view?


📈 22.13 Punkte
🔧 Programmierung

🔧 Quickly add 2FA (email) for your custom Django admin


📈 22.13 Punkte
🔧 Programmierung

🍏 How Do I Create A Vertical Display Of a Single Column In a Separate Sheet?


📈 21.51 Punkte
🍏 iOS / Mac OS

🔧 Typical Layout Case in HarmonyOS Next: Multi-column Navigation and Content Display


📈 21.51 Punkte
🔧 Programmierung

🔧 The Difference between Each Value in a Certain Column and Its Previous One and Display Result


📈 21.51 Punkte
🔧 Programmierung

🕵️ DomainMod up to 4.11.01 Custom Domain admin/domain-fields/ Add Custom cross site scripting


📈 21.44 Punkte
🕵️ Sicherheitslücken

🔧 Customizing Django Admin: A Comprehensive Guide to Overriding Admin Methods


📈 21.03 Punkte
🔧 Programmierung

🕵️ [APPSEC-2067] Admin to admin XSS in configurable custom attribute label


📈 20.34 Punkte
🕵️ Sicherheitslücken