From c66cedd32e0c0b9683ed47255c2d9f7ddc1f252e Mon Sep 17 00:00:00 2001 From: Sergey Spivak Date: Wed, 5 Feb 2020 06:27:29 +0400 Subject: [PATCH 1/4] six library import fix for Django 3 compatibility --- django_postgres_extensions/models/expressions.py | 2 +- django_postgres_extensions/models/fields/related.py | 2 +- django_postgres_extensions/models/functions.py | 2 +- django_postgres_extensions/models/sql/subqueries.py | 2 +- requirements.txt | 3 ++- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/django_postgres_extensions/models/expressions.py b/django_postgres_extensions/models/expressions.py index d395f80..71e9915 100644 --- a/django_postgres_extensions/models/expressions.py +++ b/django_postgres_extensions/models/expressions.py @@ -1,5 +1,5 @@ from django.db.models.expressions import F as BaseF, Value as BaseValue, Func, Expression -from django.utils import six +import six from django.contrib.postgres.fields.array import IndexTransform from django.utils.functional import cached_property from django.db.models.lookups import Transform diff --git a/django_postgres_extensions/models/fields/related.py b/django_postgres_extensions/models/fields/related.py index 1ac5b2a..97e8a33 100644 --- a/django_postgres_extensions/models/fields/related.py +++ b/django_postgres_extensions/models/fields/related.py @@ -6,7 +6,7 @@ from django.db import models from django.db.models.fields.related import RECURSIVE_RELATIONSHIP_CONSTANT, lazy_related_operation from django.forms.models import ModelMultipleChoiceField -from django.utils import six +import six from django.utils.encoding import force_text from .related_lookups import RelatedArrayContains, RelatedArrayExact, RelatedArrayContainedBy, RelatedContainsItem, \ RelatedArrayOverlap, RelatedAnyGreaterThan, RelatedAnyLessThanOrEqual, RelatedAnyLessThan, RelatedAnyGreaterThanOrEqual diff --git a/django_postgres_extensions/models/functions.py b/django_postgres_extensions/models/functions.py index c7ed502..bd0a8df 100644 --- a/django_postgres_extensions/models/functions.py +++ b/django_postgres_extensions/models/functions.py @@ -1,6 +1,6 @@ from django.db.models.expressions import Func, Expression from django.db.models.sql.constants import GET_ITERATOR_CHUNK_SIZE -from django.utils import six +import six from .expressions import F, Value as V class SimpleFunc(Func): diff --git a/django_postgres_extensions/models/sql/subqueries.py b/django_postgres_extensions/models/sql/subqueries.py index 1c337be..fde206d 100644 --- a/django_postgres_extensions/models/sql/subqueries.py +++ b/django_postgres_extensions/models/sql/subqueries.py @@ -1,5 +1,5 @@ from django.db.models.sql.subqueries import UpdateQuery as BaseUpdateQuery -from django.utils import six +import six from django.core.exceptions import FieldError class UpdateQuery(BaseUpdateQuery): diff --git a/requirements.txt b/requirements.txt index 68d357c..2b509cb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -django \ No newline at end of file +django +six From a592e6b7b54549a14a0799293103f1649dbc82e4 Mon Sep 17 00:00:00 2001 From: Sergey Spivak Date: Wed, 5 Feb 2020 06:30:30 +0400 Subject: [PATCH 2/4] fields import from django.contrib.postgres fixed --- django_postgres_extensions/models/fields/__init__.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/django_postgres_extensions/models/fields/__init__.py b/django_postgres_extensions/models/fields/__init__.py index 404715c..7d0270f 100644 --- a/django_postgres_extensions/models/fields/__init__.py +++ b/django_postgres_extensions/models/fields/__init__.py @@ -1,4 +1,4 @@ -from django.contrib.postgres import fields +from django.contrib.postgres import fields as django_fields from django.contrib.postgres.forms import SplitArrayField as SplitArrayFormField from django.forms.fields import TypedMultipleChoiceField from psycopg2.extras import Json @@ -9,7 +9,7 @@ from django.core import exceptions -class ArrayField(fields.ArrayField): +class ArrayField(django_fields.ArrayField): def __init__(self, base_field, form_size=None, **kwargs): super(ArrayField, self).__init__(base_field, **kwargs) @@ -38,7 +38,7 @@ def formfield(self, **kwargs): if self.choices: defaults['coerce'] = self.base_field.to_python defaults.update(kwargs) - return super(fields.ArrayField, self).formfield(**defaults) + return super(django_fields.ArrayField, self).formfield(**defaults) return super(ArrayField, self).formfield(**kwargs) def validate(self, value, model_instance): @@ -84,7 +84,7 @@ def deconstruct(self): }) return name, path, args, kwargs -class HStoreField(fields.HStoreField): +class HStoreField(django_fields.HStoreField): def __init__(self, fields=(), keys=(), max_value_length=25, require_all_fields=False, **kwargs): super(HStoreField, self).__init__(**kwargs) @@ -119,7 +119,7 @@ def formfield(self, **kwargs): defaults = kwargs return super(HStoreField, self).formfield(**defaults) -class JSONField(fields.JSONField): +class JSONField(django_fields.JSONField): def __init__(self, fields=(), require_all_fields=False, **kwargs): super(JSONField, self).__init__(**kwargs) From ae26882798974c946cbb5a54d420e37208bc9c26 Mon Sep 17 00:00:00 2001 From: Sergey Spivak Date: Wed, 5 Feb 2020 06:34:06 +0400 Subject: [PATCH 3/4] RecursionError on makemigrations with ArrayManyToMany fixed --- .../models/fields/related.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/django_postgres_extensions/models/fields/related.py b/django_postgres_extensions/models/fields/related.py index 97e8a33..1678b72 100644 --- a/django_postgres_extensions/models/fields/related.py +++ b/django_postgres_extensions/models/fields/related.py @@ -21,27 +21,27 @@ class ArrayManyToManyField(ArrayField, RelatedField): rel_class = ArrayManyToManyRel - def __init__(self, to_model, base_field=None, size=None, related_name=None, symmetrical=None, + def __init__(self, to, base_field=None, size=None, related_name=None, symmetrical=None, related_query_name=None, limit_choices_to=None, to_field=None, db_constraint=False, **kwargs): try: - to = to_model._meta.model_name + to_str = to._meta.model_name except AttributeError: - assert isinstance(to_model, six.string_types), ( + assert isinstance(to, six.string_types), ( "%s(%r) is invalid. First parameter to ForeignKey must be " "either a model, a model name, or the string %r" % ( - self.__class__.__name__, to_model, + self.__class__.__name__, to, RECURSIVE_RELATIONSHIP_CONSTANT, ) ) - to = str(to_model) + to_str = str(to) else: # For backwards compatibility purposes, we need to *try* and set # the to_field during FK construction. It won't be guaranteed to # be correct until contribute_to_class is called. Refs #12190. - to_field = to_field or (to_model._meta.pk and to_model._meta.pk.name) + to_field = to_field or (to._meta.pk and to._meta.pk.name) if not base_field: - field = to_model._meta.get_field(to_field) + field = to._meta.get_field(to_field) if not field.is_relation: base_field_type = type(field) internal_type = field.get_internal_type() @@ -58,10 +58,10 @@ def __init__(self, to_model, base_field=None, size=None, related_name=None, symm base_field = models.IntegerField() if symmetrical is None: - symmetrical = (to == RECURSIVE_RELATIONSHIP_CONSTANT) + symmetrical = (to_str == RECURSIVE_RELATIONSHIP_CONSTANT) kwargs['rel'] = self.rel_class( - self, to, to_field, + self, to_str, to_field, related_name=related_name, related_query_name=related_query_name, limit_choices_to=limit_choices_to, @@ -71,10 +71,10 @@ def __init__(self, to_model, base_field=None, size=None, related_name=None, symm self.db_constraint = db_constraint - self.to = to + self.to = to_str if 'default' not in kwargs.keys(): - kwargs['default'] = [] + kwargs['default'] = list kwargs['blank'] = True self.from_fields = ['self'] @@ -84,15 +84,15 @@ def __init__(self, to_model, base_field=None, size=None, related_name=None, symm def deconstruct(self): name, path, args, kwargs = super(ArrayManyToManyField, self).deconstruct() - args = (self.to,) kwargs.update({ + 'to': self.to, 'base_field': self.base_field, 'size': self.size, 'related_name': self.remote_field.related_name, 'symmetrical': self.remote_field.symmetrical, 'related_query_name': self.remote_field.related_query_name, 'limit_choices_to': self.remote_field.limit_choices_to, - 'to_field': self.remote_field.field, + 'to_field': self.remote_field.field_name, 'db_constraint': self.db_constraint }) return name, path, args, kwargs From b02dbc8baac6ab41656abcbd07f17c9370dc03fb Mon Sep 17 00:00:00 2001 From: Sergey Spivak Date: Wed, 5 Feb 2020 06:34:48 +0400 Subject: [PATCH 4/4] minor ArrayManyToMany documentation fix --- docs/arraym2m.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/arraym2m.rst b/docs/arraym2m.rst index 1b310e5..a4b00b9 100644 --- a/docs/arraym2m.rst +++ b/docs/arraym2m.rst @@ -15,7 +15,7 @@ To use this field, it is required to set ENABLE_ARRAY_M2M = True in settings.py Then in models.py:: from django.db import models - from django_postgres_extensions.models.fields import ArrayManyToManyField + from django_postgres_extensions.models import ArrayManyToManyField class Publication(models.Model): title = models.CharField(max_length=30)