Skip to main content

A Django app for dynamic admin list views.

Project description

# Django Auto ListView

A Django app to dynamically create `list_display` fields in the admin for all models in your app.

## Installation

You can install `django-auto-listview` either locally (for development purposes) or from PyPI (if it's published).

### Install from PyPI

To install the package from PyPI, run:

```bash
pip install django-auto-listview

Install Locally (For Development)

To install the package locally in development mode (editable install), run:

pip install -e /path/to/django-auto-listview

Replace /path/to/django-auto-listview with the path to the django-auto-listview directory.

Usage

After installing the package, follow these steps to integrate it into your Django project.

1. Add django_auto_listview to INSTALLED_APPS

In your settings.py, add django_auto_listview to the INSTALLED_APPS list:

INSTALLED_APPS = [
    # Other apps
    'django_auto_listview',
    'firstapp',  # Your app (e.g., 'firstapp')
]

2. Register the Dynamic Admin

In the admin.py file of your app (e.g., firstapp/admin.py), register the dynamic admin by calling register_dynamic_admin():

from django.contrib import admin
from django_auto_listview.admin import register_dynamic_admin

# Register the dynamic admin for all models in the app
register_dynamic_admin()

This will automatically apply the DynamicModelAdmin to all models in your app.

3. Define Models

Ensure that you have models defined in your app. For example, in firstapp/models.py, define a model like this:

from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField(blank=True, null=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    created_at = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return self.name

4. Run Migrations

After defining your models, run the following commands to create the necessary database tables:

python manage.py makemigrations
python manage.py migrate

5. Access the Django Admin

  1. Create a superuser if you haven’t already:

    python manage.py createsuperuser
    
  2. Start the Django development server:

    python manage.py runserver
    
  3. Visit the Django admin panel at http://127.0.0.1:8000/admin/ and log in with your superuser credentials.

  4. You should now see the models (e.g., Product) listed in the admin panel. The DynamicModelAdmin will dynamically generate list_display fields based on the model’s fields, excluding fields that are blank=True and null=False.

Customizing the Dynamic Admin

The dynamic admin behavior is based on the fields of your model. If you need to customize how the list_display works (e.g., including/excluding certain fields, or altering the display format), you can override the methods of the DynamicModelAdmin class.

To customize the behavior, subclass DynamicModelAdmin in your admin.py file like so:

from django.contrib import admin
from django_auto_listview.admin import DynamicModelAdmin

class CustomProductAdmin(DynamicModelAdmin):
    def get_list_display(self, request):
        # Add custom fields or modify the default behavior
        list_display = super().get_list_display(request)
        list_display.append('price')  # Add 'price' field explicitly
        return list_display

admin.site.register(Product, CustomProductAdmin)

This allows you to extend or override any default functionality, such as adding extra fields to the list_display or changing how fields are displayed.

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_auto_listview-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

django_auto_listview-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file django_auto_listview-0.1.0.tar.gz.

File metadata

  • Download URL: django_auto_listview-0.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for django_auto_listview-0.1.0.tar.gz
Algorithm Hash digest
SHA256 daeceee1b7fbd5aa9bae6f4470c17c52cc46524ffb5b98d722f11b295101e473
MD5 9814564a0fdcb6aa7859cc07f143153a
BLAKE2b-256 a379e6c8ec8b965a44fc6872918221dad16ba6585402517cb881c54a0655ff2c

See more details on using hashes here.

File details

Details for the file django_auto_listview-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_auto_listview-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5fe027b05e100fca9f7b85cec2a05ff343dd614eb19d13eaf882ae6357b82761
MD5 ed48ad59fafefc4657fdcec8f825cb91
BLAKE2b-256 ac3da0e39412f4bea4de6dcd10a413ac3ae93e93851d2efba0b0470459467249

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