Skip to content

Commit

Permalink
Add explanations to imports in generators.py (#179)
Browse files Browse the repository at this point in the history
To match with currently supported Django versions (2.2+).

* Add explanations to imports in `generators.py`
* Fix typo, move to 'added'
  • Loading branch information
amureki authored Apr 17, 2021
1 parent 73943d8 commit 59fc369
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased](https://github.com/model-bakers/model_bakery/tree/main)

### Added
- [dev] Add explanations to imports in `generators.py` to match with current supported Django versions [PR #179](https://github.com/model-bakers/model_bakery/pull/179)

### Changed

Expand Down
20 changes: 15 additions & 5 deletions model_bakery/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,47 @@
from .utils import import_from_str

try:
# Proper support starts with Django 3.0
# (as it uses `django/db/backends/base/operations.py` for matching ranges)
from django.db.models import AutoField, BigAutoField, SmallAutoField
except ImportError:
AutoField = None
BigAutoField = None
SmallAutoField = None

try:
# added in Django 3.1
from django.db.models import PositiveBigIntegerField
except ImportError:
PositiveBigIntegerField = None

try:
# Replaced `django.contrib.postgres.fields.JSONField` in Django 3.1
from django.db.models import JSONField
except ImportError:
JSONField = None

try:
# PostgreSQL-specific field (only available when psycopg2 is installed)
from django.contrib.postgres.fields import ArrayField
except ImportError:
ArrayField = None

try:
# Deprecated since Django 3.1
# PostgreSQL-specific field (only available when psycopg2 is installed)
from django.contrib.postgres.fields import JSONField as PostgresJSONField
except ImportError:
PostgresJSONField = None

try:
# PostgreSQL-specific field (only available when psycopg2 is installed)
from django.contrib.postgres.fields import HStoreField
except ImportError:
HStoreField = None

try:
# PostgreSQL-specific fields (only available when psycopg2 is installed)
from django.contrib.postgres.fields.citext import (
CICharField,
CIEmailField,
Expand All @@ -79,22 +88,23 @@
CITextField = None

try:
from django.contrib.postgres.fields.ranges import DecimalRangeField
except ImportError:
DecimalRangeField = None
try:
# PostgreSQL-specific fields (only available when psycopg2 is installed)
from django.contrib.postgres.fields.ranges import (
BigIntegerRangeField,
DateRangeField,
DateTimeRangeField,
DecimalRangeField,
IntegerRangeField,
)
except ImportError:
IntegerRangeField = None
BigIntegerRangeField = None
DateRangeField = None
DateTimeRangeField = None
DecimalRangeField = None
IntegerRangeField = None

try:
# Deprecated since Django 2.2
from django.contrib.postgres.fields.ranges import FloatRangeField
except ImportError:
FloatRangeField = None
Expand Down
6 changes: 1 addition & 5 deletions tests/test_filling_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,6 @@ def test_filling_citextfield(self, person):
assert isinstance(ci_text_field, CITextField)
assert isinstance(person.ci_text, str)

@pytest.mark.skipif(
not DecimalRangeField,
reason="Django version does not support DecimalRangeField",
)
def test_filling_decimal_range_field(self, person):
from psycopg2._range import NumericRange

Expand Down Expand Up @@ -489,7 +485,7 @@ def test_filling_integer_range_field_for_big_int(self, person):

@pytest.mark.skipif(
FloatRangeField is None,
reason="Django version does not support FloatRangeField",
reason="FloatRangeField is deprecated since Django 2.2",
)
def test_filling_float_range_field(self, person):
from psycopg2._range import NumericRange
Expand Down

0 comments on commit 59fc369

Please sign in to comment.