Coder Social home page Coder Social logo

edsonibarra / django-admin-list-filter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vigo/django-admin-list-filter

0.0 0.0 0.0 9.51 MB

Dead simple autocompletion for Django admin `list_filter`.

Home Page: https://pypi.org/project/dalf/

License: MIT License

JavaScript 41.59% Ruby 9.12% Python 30.95% CSS 0.85% HTML 17.49%

django-admin-list-filter's Introduction

Version Python Django Ruff PyPI version PyPI - Downloads

Django Admin List Filter

Dead simple autocompletion for Django admin list_filter. This was made using the libraries shipped with Django (select2, jquery), Django’s built-in list filters, and Django’s built-in AutocompleteJsonView.

This package is an improved version of the previously created django-admin-autocomplete-list-filter package. It supports Django version 5 and above. Please note that the django-admin-autocomplete-list-filter package is now deprecated. Since I am no longer part of the organization where it was initially developed, I cannot archive it.

No extra package or install required!

Before Django Admin List Filter

Before Django Admin List Filter

After Django Admin List Filter

After Django Admin List Filter


Installation

pip install django-admin-list-filter

Add dalf to your INSTALLED_APPS in your settings.py:

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "dalf", # <- add
]

Usage

Use DALFModelAdmin, inherited from admin.ModelAdmin to inject media urls only. You have some filters;

  • DALFRelatedField: inherited from admin.RelatedFieldListFilter.
  • DALFRelatedFieldAjax: inherited from admin.RelatedFieldListFilter
  • DALFRelatedOnlyField: inherited from admin.RelatedOnlyFieldListFilter.
  • DALFChoicesField: inherited from admin.ChoicesFieldListFilter.

Example models.py

# models.py
import uuid

from django.conf import settings
from django.db import models


class Category(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=255)

    def __str__(self):
        return self.title


class Tag(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name


class Post(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    category = models.ForeignKey(to='Category', on_delete=models.CASCADE, related_name='posts')
    author = models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='posts')
    title = models.CharField(max_length=255)
    body = models.TextField()
    tags = models.ManyToManyField(to='Tag', blank=True)

    def __str__(self):
        return self.title

Example admin.py:

# admin.py
from dalf.admin import DALFModelAdmin, DALFRelatedOnlyField, DALFRelatedFieldAjax
from django.contrib import admin


@admin.register(Post)
class PostAdmin(DALFModelAdmin):
    list_filter = (
        ('author', DALFRelatedOnlyField),    # if author has a post!
        ('category', DALFRelatedFieldAjax),  # enable ajax completion for category field (FK)
        ('tags', DALFRelatedFieldAjax),      # enable ajax completion for tags field (M2M)
    )

That’s all... There is also DALFChoicesField, you can test it out:

# admin.py
from dalf.admin import DALFModelAdmin, DALFChoicesField, DALFRelatedOnlyField, DALFRelatedFieldAjax
from django.contrib import admin


@admin.register(Post)
class PostAdmin(DALFModelAdmin):
    list_filter = (
        ('author', DALFChoicesField),        # enable autocomplete w/o ajax (FK)
        ('category', DALFRelatedFieldAjax),  # enable ajax completion for category field (FK)
        ('tags', DALFRelatedOnlyField),      # enable ajax completion for tags field (M2M) if posts has any tag!
    )

Extras

I mostly use django-timezone-field, here is an illustration of timezone completion w/o ajax:

pip install django-timezone-field

Now add timezone field to Post model:

# modify models.py, add new ones
from timezone_field import TimeZoneField                 # <- add this line

class Post(models.Model):
    # all the other fiels
    timezone = TimeZoneField(default=settings.TIME_ZONE) # <- add this line
    
    # rest of the code

Now, just add timezone as regular list_filter:

# modify admin.py, add new ones

@admin.register(Post)
class PostAdmin(DALFModelAdmin):
    # previous codes
    list_filter = (
        # previous filters
        ('timezone', DALFChoicesField), # <- add this line
    )

That’s it!


Contributor(s)


Contribute

All PR’s are welcome!

  1. fork (https://github.com/vigo/django-admin-list-filter/fork)
  2. Create your branch (git checkout -b my-feature)
  3. commit yours (git commit -am 'add some functionality')
  4. push your branch (git push origin my-feature)
  5. Than create a new Pull Request!

I am not very proficient in JavaScript. Therefore, any support, suggestions, and feedback are welcome to help improve the project. Feel free to open pull requests!


Development

Clone the repo somewhere, and install with:

pip install -e /path/to/django-admin-list-filter

And play with the filters :)

Publish

Note to my self:

pip install build twine
rake -T

rake build           # Build package
rake bump[revision]  # Bump version: major,minor,patch
rake clean           # Remove/Delete build..
rake upload:main     # Upload package to main distro (release)
rake upload:test     # Upload package to test distro

Change Log

2024-05-20

  • Initial release.

You can read the whole story here.


License

This project is licensed under MIT


This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

django-admin-list-filter's People

Contributors

vigo 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.