diff --git a/README.md b/README.md index 08bb6f1..41510f2 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,14 @@ All PR’s are welcome! 1. `push` your `branch` (`git push origin my-features`) 1. Than create a new **Pull Request**! +Running the example project: + +1. `pip install django` +1. `./manage.py migrate` +1. `./manage.py createsuperuser` - set up an admin user to your liking +1. `/manage.py runserver` +1. Sign in at http://127.0.0.1:8000/admin/ + --- ## TODO diff --git a/djaa_list_filter_example/__init__.py b/djaa_list_filter_example/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/djaa_list_filter_example/admin.py b/djaa_list_filter_example/admin.py new file mode 100644 index 0000000..0ad5fcf --- /dev/null +++ b/djaa_list_filter_example/admin.py @@ -0,0 +1,28 @@ +from django.contrib import admin + +from djaa_list_filter.admin import ( + AjaxAutocompleteListFilterModelAdmin, +) + +from .models import Category, Post, Tag + + +@admin.register(Post) +class PostAdmin(AjaxAutocompleteListFilterModelAdmin): + list_display = ('__str__', 'author', 'show_tags') + autocomplete_list_filter = ('category', 'author', 'tags') + + def show_tags(self, obj): + return ' , '.join(obj.tags.values_list('name', flat=True)) + + +@admin.register(Category) +class CategoryAdmin(admin.ModelAdmin): + search_fields = ['title'] + ordering = ['title'] + + +@admin.register(Tag) +class TagAdmin(admin.ModelAdmin): + search_fields = ['name'] + ordering = ['name'] diff --git a/djaa_list_filter_example/asgi.py b/djaa_list_filter_example/asgi.py new file mode 100644 index 0000000..55bf528 --- /dev/null +++ b/djaa_list_filter_example/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for djaa_list_filter_example project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djaa_list_filter_example.settings') + +application = get_asgi_application() diff --git a/djaa_list_filter_example/migrations/0001_initial.py b/djaa_list_filter_example/migrations/0001_initial.py new file mode 100644 index 0000000..9732f23 --- /dev/null +++ b/djaa_list_filter_example/migrations/0001_initial.py @@ -0,0 +1,42 @@ +# Generated by Django 3.2.6 on 2021-08-27 08:03 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Category', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=255)), + ], + ), + migrations.CreateModel( + name='Tag', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ], + ), + migrations.CreateModel( + name='Post', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=255)), + ('body', models.TextField()), + ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='posts', to=settings.AUTH_USER_MODEL)), + ('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='posts', to='djaa_list_filter_example.category')), + ('tags', models.ManyToManyField(blank=True, to='djaa_list_filter_example.Tag')), + ], + ), + ] diff --git a/djaa_list_filter_example/migrations/__init__.py b/djaa_list_filter_example/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/djaa_list_filter_example/models.py b/djaa_list_filter_example/models.py new file mode 100644 index 0000000..e5d36e0 --- /dev/null +++ b/djaa_list_filter_example/models.py @@ -0,0 +1,27 @@ +from django.conf import settings +from django.db import models + + +class Post(models.Model): + 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 + + +class Category(models.Model): + title = models.CharField(max_length=255) + + def __str__(self): + return self.title + + +class Tag(models.Model): + name = models.CharField(max_length=255) + + def __str__(self): + return self.name diff --git a/djaa_list_filter_example/settings.py b/djaa_list_filter_example/settings.py new file mode 100644 index 0000000..7a5f86b --- /dev/null +++ b/djaa_list_filter_example/settings.py @@ -0,0 +1,128 @@ +""" +Django settings for djaa_list_filter_example project. + +Generated by 'django-admin startproject' using Django 3.2.6. + +For more information on this file, see +https://docs.djangoproject.com/en/3.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.2/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-#pd+bfn1jgqgo4nb-ou7!-fyxo69l=0av3z_8#6th9^4(n2s3$' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + [ + 'djaa_list_filter', + 'djaa_list_filter_example', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'djaa_list_filter_example.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'djaa_list_filter_example.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.2/howto/static-files/ + +STATIC_URL = '/static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/djaa_list_filter_example/urls.py b/djaa_list_filter_example/urls.py new file mode 100644 index 0000000..8858022 --- /dev/null +++ b/djaa_list_filter_example/urls.py @@ -0,0 +1,21 @@ +"""djaa_list_filter_example URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/djaa_list_filter_example/wsgi.py b/djaa_list_filter_example/wsgi.py new file mode 100644 index 0000000..9765b1c --- /dev/null +++ b/djaa_list_filter_example/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for djaa_list_filter_example project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djaa_list_filter_example.settings') + +application = get_wsgi_application() diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..d25a19d --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djaa_list_filter_example.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main()