From 8bfad8c112106803ed9cbff67678f80d019be5af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Salomv=C3=A1ry?= Date: Fri, 27 Aug 2021 10:05:06 +0200 Subject: [PATCH 01/10] Add example project --- djaa_list_filter_example/__init__.py | 0 djaa_list_filter_example/admin.py | 28 ++++ djaa_list_filter_example/asgi.py | 16 +++ .../migrations/0001_initial.py | 42 ++++++ .../migrations/__init__.py | 0 djaa_list_filter_example/models.py | 27 ++++ djaa_list_filter_example/settings.py | 128 ++++++++++++++++++ djaa_list_filter_example/urls.py | 21 +++ djaa_list_filter_example/wsgi.py | 16 +++ manage.py | 22 +++ 10 files changed, 300 insertions(+) create mode 100644 djaa_list_filter_example/__init__.py create mode 100644 djaa_list_filter_example/admin.py create mode 100644 djaa_list_filter_example/asgi.py create mode 100644 djaa_list_filter_example/migrations/0001_initial.py create mode 100644 djaa_list_filter_example/migrations/__init__.py create mode 100644 djaa_list_filter_example/models.py create mode 100644 djaa_list_filter_example/settings.py create mode 100644 djaa_list_filter_example/urls.py create mode 100644 djaa_list_filter_example/wsgi.py create mode 100755 manage.py 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() From 31b3f0715eb5397093ae0ceaed6e203a6a1dbf81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Salomv=C3=A1ry?= Date: Fri, 27 Aug 2021 10:28:18 +0200 Subject: [PATCH 02/10] Fix query param name for custom pk name If the related model's primary key is not named `id`, use the right primary key name when constructing the query param name. --- djaa_list_filter/admin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/djaa_list_filter/admin.py b/djaa_list_filter/admin.py index 6ae6fbf..4c4ce7d 100644 --- a/djaa_list_filter/admin.py +++ b/djaa_list_filter/admin.py @@ -52,7 +52,8 @@ class AjaxAutocompleteListFilter(admin.RelatedFieldListFilter): def __init__(self, field, request, params, model, model_admin, field_path): super().__init__(field, request, params, model, model_admin, field_path) - qs_target_value = self.parameter_name % (field.name, model._meta.pk.name) + related_model = field.related_model + qs_target_value = self.parameter_name % (field.name, related_model._meta.pk.name) queryset = self.get_queryset_for_field(model, field.name) widget = AjaxAutocompleteSelectWidget( model_admin=model_admin, model=model, field_name=field.name, qs_target_value=qs_target_value From 98ad14f1ed0d292c280d23ac686edf95ca2b2730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Salomv=C3=A1ry?= Date: Fri, 27 Aug 2021 10:30:44 +0200 Subject: [PATCH 03/10] Get QuerySet from field.related_model directly I do not see any reason for the custom related model resolution logic. But maybe I don't understand the intent... --- djaa_list_filter/admin.py | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/djaa_list_filter/admin.py b/djaa_list_filter/admin.py index 4c4ce7d..4e6fe60 100644 --- a/djaa_list_filter/admin.py +++ b/djaa_list_filter/admin.py @@ -54,7 +54,7 @@ def __init__(self, field, request, params, model, model_admin, field_path): related_model = field.related_model qs_target_value = self.parameter_name % (field.name, related_model._meta.pk.name) - queryset = self.get_queryset_for_field(model, field.name) + queryset = related_model.objects.get_queryset() widget = AjaxAutocompleteSelectWidget( model_admin=model_admin, model=model, field_name=field.name, qs_target_value=qs_target_value ) @@ -69,21 +69,6 @@ class AutocompleteForm(forms.Form): initial_values.update(autocomplete_field=autocomplete_field_initial_value) self.autocomplete_form = AutocompleteForm(initial=initial_values, prefix=field.name) - def get_queryset_for_field(self, model, name): - """ - Thanks to farhan0581 - https://github.com/farhan0581/django-admin-autocomplete-filter/blob/master/admin_auto_filters/filters.py - """ - - field_desc = getattr(model, name) - if isinstance(field_desc, ManyToManyDescriptor): - related_model = field_desc.rel.related_model if field_desc.reverse else field_desc.rel.model - elif isinstance(field_desc, ReverseManyToOneDescriptor): - related_model = field_desc.rel.related_model - else: - return field_desc.get_queryset() - return related_model.objects.get_queryset() - class AjaxAutocompleteListFilterModelAdmin(admin.ModelAdmin): def get_list_filter(self, request): From 45d15f5d5e15ba368708198874f035b3390caa50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Salomv=C3=A1ry?= Date: Thu, 2 Sep 2021 18:31:16 +0200 Subject: [PATCH 04/10] Unbreak "normal" autocomplete lists Do not apply the `width: 100% !important` rule to select2 components on regular admin forms. --- .../djaa_list_filter/admin/css/autocomplete_list_filter.css | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/djaa_list_filter/static/djaa_list_filter/admin/css/autocomplete_list_filter.css b/djaa_list_filter/static/djaa_list_filter/admin/css/autocomplete_list_filter.css index 4d24846..7793b19 100644 --- a/djaa_list_filter/static/djaa_list_filter/admin/css/autocomplete_list_filter.css +++ b/djaa_list_filter/static/djaa_list_filter/admin/css/autocomplete_list_filter.css @@ -1,3 +1,3 @@ -.select2-container--admin-autocomplete { +.ajax-autocomplete-select-widget-wrapper .select2-container--admin-autocomplete { width: 100% !important; -} \ No newline at end of file +} From 777065af327e0c0f91fe3b8a0525995fc03bbba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Salomv=C3=A1ry?= Date: Thu, 2 Sep 2021 18:31:16 +0200 Subject: [PATCH 05/10] Unbreak "normal" autocomplete lists Do not apply the `width: 100% !important` rule to select2 components on regular admin forms. --- .../djaa_list_filter/admin/css/autocomplete_list_filter.css | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/djaa_list_filter/static/djaa_list_filter/admin/css/autocomplete_list_filter.css b/djaa_list_filter/static/djaa_list_filter/admin/css/autocomplete_list_filter.css index 4d24846..7793b19 100644 --- a/djaa_list_filter/static/djaa_list_filter/admin/css/autocomplete_list_filter.css +++ b/djaa_list_filter/static/djaa_list_filter/admin/css/autocomplete_list_filter.css @@ -1,3 +1,3 @@ -.select2-container--admin-autocomplete { +.ajax-autocomplete-select-widget-wrapper .select2-container--admin-autocomplete { width: 100% !important; -} \ No newline at end of file +} From cfd0c3e4d9c431f43c2fef8bb272a15aa1d765e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Salomv=C3=A1ry?= Date: Thu, 6 Jan 2022 10:02:42 +0100 Subject: [PATCH 06/10] Stop using ugettext_lazy removed in Django 4 --- djaa_list_filter/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/djaa_list_filter/admin.py b/djaa_list_filter/admin.py index 6ae6fbf..d9af2a1 100644 --- a/djaa_list_filter/admin.py +++ b/djaa_list_filter/admin.py @@ -10,7 +10,7 @@ ManyToManyDescriptor, ReverseManyToOneDescriptor, ) -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ CURRENT_PYTHON = sys.version_info[:2] REQUIRED_PYTHON_FOR_FSTRING = (3, 6) From 629f86b00d1815a11a4162dd9bd5cba7bb62aa57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Salomv=C3=A1ry?= Date: Fri, 21 Oct 2022 12:41:17 +0200 Subject: [PATCH 07/10] Prevent loading all choices on template render https://github.com/demiroren-teknoloji/django-admin-autocomplete-list-filter/issues/14 --- djaa_list_filter/admin.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/djaa_list_filter/admin.py b/djaa_list_filter/admin.py index 339d16e..5bf45c2 100644 --- a/djaa_list_filter/admin.py +++ b/djaa_list_filter/admin.py @@ -68,6 +68,18 @@ class AutocompleteForm(forms.Form): if autocomplete_field_initial_value: initial_values.update(autocomplete_field=autocomplete_field_initial_value) self.autocomplete_form = AutocompleteForm(initial=initial_values, prefix=field.name) + self.choices_count = queryset.count() + + def field_choices(self, field, request, model_admin): + # No choices are to be rendered in the template + return [] + + def has_output(self): + if self.include_empty_choice: + extra = 1 + else: + extra = 0 + return self.choices_count + extra > 1 class AjaxAutocompleteListFilterModelAdmin(admin.ModelAdmin): From e3769ff7360028b2de030dec025c09f6c6158217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Salomv=C3=A1ry?= Date: Fri, 27 Aug 2021 10:05:06 +0200 Subject: [PATCH 08/10] Add example project --- djaa_list_filter_example/__init__.py | 0 djaa_list_filter_example/admin.py | 28 ++++ djaa_list_filter_example/asgi.py | 16 +++ .../migrations/0001_initial.py | 42 ++++++ .../migrations/__init__.py | 0 djaa_list_filter_example/models.py | 27 ++++ djaa_list_filter_example/settings.py | 128 ++++++++++++++++++ djaa_list_filter_example/urls.py | 21 +++ djaa_list_filter_example/wsgi.py | 16 +++ manage.py | 22 +++ 10 files changed, 300 insertions(+) create mode 100644 djaa_list_filter_example/__init__.py create mode 100644 djaa_list_filter_example/admin.py create mode 100644 djaa_list_filter_example/asgi.py create mode 100644 djaa_list_filter_example/migrations/0001_initial.py create mode 100644 djaa_list_filter_example/migrations/__init__.py create mode 100644 djaa_list_filter_example/models.py create mode 100644 djaa_list_filter_example/settings.py create mode 100644 djaa_list_filter_example/urls.py create mode 100644 djaa_list_filter_example/wsgi.py create mode 100755 manage.py 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() From 44b7f8abefc50a53de5e7d1eb068c45c737cb18b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Salomv=C3=A1ry?= Date: Fri, 21 Oct 2022 12:45:40 +0200 Subject: [PATCH 09/10] Document running the example project --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) 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 From 2e5ebdeafaead9250bab7c2cd57472fb344bf9d5 Mon Sep 17 00:00:00 2001 From: Attila Szabo Date: Mon, 22 Sep 2025 10:12:55 +0200 Subject: [PATCH 10/10] Remove linked pylintrc and add a minimal one --- .pylintrc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) mode change 120000 => 100644 .pylintrc diff --git a/.pylintrc b/.pylintrc deleted file mode 120000 index ae9e54b..0000000 --- a/.pylintrc +++ /dev/null @@ -1 +0,0 @@ -/Users/vigo/Repos/Dropbox/Files/configs/pylintrc \ No newline at end of file diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..db74f85 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,7 @@ +[MASTER] +# Minimal pylintrc to satisfy Heroku buildpack cache copy +ignore=CVS + +[MESSAGES CONTROL] +disable=all +