From 2010b49d0687ae67a249da81d4cfff2656d54465 Mon Sep 17 00:00:00 2001 From: Jan-Jelle Kester Date: Wed, 22 Apr 2020 23:00:12 +0200 Subject: [PATCH] Fix Django 3.0 field choices diff --- auditlog/models.py | 6 +++--- auditlog_tests/test_settings.py | 11 ++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/auditlog/models.py b/auditlog/models.py index 4d69387..f04e562 100644 --- a/auditlog/models.py +++ b/auditlog/models.py @@ -8,7 +8,7 @@ from django.contrib.contenttypes.fields import GenericRelation from django.contrib.contenttypes.models import ContentType from django.core.exceptions import FieldDoesNotExist from django.db import models, DEFAULT_DB_ALIAS -from django.db.models import QuerySet, Q +from django.db.models import QuerySet, Q, Field from django.utils import formats, timezone from django.utils.encoding import smart_str from django.utils.translation import ugettext_lazy as _ @@ -258,9 +258,9 @@ class LogEntry(models.Model): values_display = [] # handle choices fields and Postgres ArrayField to get human readable version choices_dict = None - if hasattr(field, 'choices') and len(field.choices) > 0: + if getattr(field, 'choices') and len(field.choices) > 0: choices_dict = dict(field.choices) - if hasattr(field, 'base_field') and getattr(field.base_field, 'choices', False): + if hasattr(field, 'base_field') and isinstance(field.base_field, Field) and getattr(field.base_field, 'choices') and len(field.base_field.choices) > 0: choices_dict = dict(field.base_field.choices) if choices_dict: diff --git a/auditlog_tests/test_settings.py b/auditlog_tests/test_settings.py index fdd2f2a..e8ac594 100644 --- a/auditlog_tests/test_settings.py +++ b/auditlog_tests/test_settings.py @@ -1,6 +1,7 @@ """ Settings file for the Auditlog test suite. """ +import os SECRET_KEY = 'test' @@ -25,11 +26,11 @@ MIDDLEWARE = ( DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', - 'NAME': 'auditlog_tests_db', - 'USER': 'postgres', - 'PASSWORD': '', - 'HOST': '127.0.0.1', - 'PORT': '5432', + 'NAME': os.getenv('TEST_DB_NAME', 'auditlog_tests_db'), + 'USER': os.getenv('TEST_DB_USER', 'postgres'), + 'PASSWORD': os.getenv('TEST_DB_PASS', ''), + 'HOST': os.getenv('TEST_DB_HOST', '127.0.0.1'), + 'PORT': os.getenv('TEST_DB_PORT', '5432'), } }