Skip to content

Commit

Permalink
Drop Django 3.2 support (#475)
Browse files Browse the repository at this point in the history
It reached EOL in April 2024
  • Loading branch information
amureki authored Apr 17, 2024
1 parent c972cbb commit 6d8eaee
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 110 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased](https://github.com/model-bakers/model_bakery/tree/main)

### Added

- Add Django 5.0 support

### Changed

### Removed
- Drop Django 4.1 support (reached end of life)
- Drop Django 3.2 and 4.1 support (reached end of life)

## [1.17.0](https://pypi.org/project/model-bakery/1.17.0/)

Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import sys

sys.path.insert(0, os.path.abspath(".."))
from model_bakery import __about__

from model_bakery import __about__ # noqa
sys.path.insert(0, os.path.abspath(".."))

project = "Model Bakery"
copyright = "2023, Rust Saiargaliev"
Expand Down
6 changes: 3 additions & 3 deletions docs/how_bakery_behaves.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Model Bakery should handle fields that:

## Currently supported fields

- `BooleanField`, `NullBooleanField`, `IntegerField`, `BigIntegerField`, `SmallIntegerField`, `PositiveIntegerField`, `PositiveSmallIntegerField`, `FloatField`, `DecimalField`
- `BooleanField`, `IntegerField`, `BigIntegerField`, `SmallIntegerField`, `PositiveIntegerField`, `PositiveBigIntegerField`, `PositiveSmallIntegerField`, `FloatField`, `DecimalField`
- `CharField`, `TextField`, `BinaryField`, `SlugField`, `URLField`, `EmailField`, `IPAddressField`, `GenericIPAddressField`, `ContentType`
- `ForeignKey`, `OneToOneField`, `ManyToManyField` (even with through model)
- `DateField`, `DateTimeField`, `TimeField`, `DurationField`
Expand Down Expand Up @@ -93,7 +93,7 @@ class CustomBaker(baker.Baker):
return [
field
for field in super(CustomBaker, self).get_fields()
if not field isinstance CustomField
if not isinstance(field, CustomField)
]

# in your settings.py file:
Expand All @@ -111,7 +111,7 @@ movie = baker.make(Movie, title='Old Boys', _from_manager='availables') # This
If you have overwritten the `save` method for a model, you can pass custom parameters to it using Model Bakery. Example:

```python
class ProjectWithCustomSave(models.Model)
class ProjectWithCustomSave(models.Model):
# some model fields
created_by = models.ForeignKey(settings.AUTH_USER_MODEL)

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Model Bakery is a rename of the legacy [model_mommy\'s project](https://pypi.org

# Compatibility

Model Bakery supports Django \>= 3.2.
Model Bakery supports Django \>= 4.2.

# Install

Expand Down
18 changes: 4 additions & 14 deletions model_bakery/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
overload,
)

from django import VERSION as DJANGO_VERSION
from django.apps import apps
from django.conf import settings
from django.contrib import contenttypes
Expand Down Expand Up @@ -78,8 +77,7 @@ def make(
_using: str = "",
_bulk_create: bool = False,
**attrs: Any,
) -> M:
...
) -> M: ...


@overload
Expand All @@ -94,8 +92,7 @@ def make(
_bulk_create: bool = False,
_fill_optional: Union[List[str], bool] = False,
**attrs: Any,
) -> List[M]:
...
) -> List[M]: ...


def make(
Expand Down Expand Up @@ -147,8 +144,7 @@ def prepare(
_save_related: bool = False,
_using: str = "",
**attrs,
) -> M:
...
) -> M: ...


@overload
Expand All @@ -159,8 +155,7 @@ def prepare(
_using: str = "",
_fill_optional: Union[List[str], bool] = False,
**attrs,
) -> List[M]:
...
) -> List[M]: ...


def prepare(
Expand Down Expand Up @@ -818,12 +813,7 @@ def bulk_create(baker: Baker[M], quantity: int, **kwargs) -> List[M]:
else:
manager = baker.model._base_manager

existing_entries = list(manager.values_list("pk", flat=True))
created_entries = manager.bulk_create(entries)
# bulk_create in Django < 4.0 does not return ids of created objects.
# drop this after 01 Apr 2024 (Django 3.2 LTS end of life)
if DJANGO_VERSION < (4, 0):
created_entries = manager.exclude(pk__in=existing_entries)

# set many-to-many relations from kwargs
for entry in created_entries:
Expand Down
60 changes: 10 additions & 50 deletions model_bakery/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from django.db.backends.base.operations import BaseDatabaseOperations
from django.db.models import (
AutoField,
BigAutoField,
BigIntegerField,
BinaryField,
BooleanField,
Expand All @@ -19,11 +21,14 @@
ImageField,
IntegerField,
IPAddressField,
JSONField,
ManyToManyField,
OneToOneField,
PositiveBigIntegerField,
PositiveIntegerField,
PositiveSmallIntegerField,
SlugField,
SmallAutoField,
SmallIntegerField,
TextField,
TimeField,
Expand All @@ -34,40 +39,12 @@
from . import random_gen
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 psycopg is installed)
from django.contrib.postgres.fields import ArrayField
except ImportError:
ArrayField = None

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

try:
# PostgreSQL-specific field (only available when psycopg is installed)
from django.contrib.postgres.fields import HStoreField
Expand All @@ -86,12 +63,6 @@
CIEmailField = None
CITextField = None

try:
# Deprecated since Django 3.1, removed in Django 4.0
from django.db.models import NullBooleanField
except ImportError:
NullBooleanField = None


try:
# PostgreSQL-specific fields (only available when psycopg is installed)
Expand Down Expand Up @@ -124,9 +95,13 @@ def gen_integer():
OneToOneField: random_gen.gen_related,
ManyToManyField: random_gen.gen_m2m,
BooleanField: random_gen.gen_boolean,
AutoField: _make_integer_gen_by_range(AutoField),
BigAutoField: _make_integer_gen_by_range(BigAutoField),
IntegerField: _make_integer_gen_by_range(IntegerField),
SmallAutoField: _make_integer_gen_by_range(SmallAutoField),
BigIntegerField: _make_integer_gen_by_range(BigIntegerField),
SmallIntegerField: _make_integer_gen_by_range(SmallIntegerField),
PositiveBigIntegerField: _make_integer_gen_by_range(PositiveBigIntegerField),
PositiveIntegerField: _make_integer_gen_by_range(PositiveIntegerField),
PositiveSmallIntegerField: _make_integer_gen_by_range(PositiveSmallIntegerField),
FloatField: random_gen.gen_float,
Expand All @@ -146,14 +121,11 @@ def gen_integer():
FileField: random_gen.gen_file_field,
ImageField: random_gen.gen_image_field,
DurationField: random_gen.gen_interval,
JSONField: random_gen.gen_json,
} # type: Dict[Type, Callable]

if ArrayField:
default_mapping[ArrayField] = random_gen.gen_array
if JSONField:
default_mapping[JSONField] = random_gen.gen_json
if PostgresJSONField:
default_mapping[PostgresJSONField] = random_gen.gen_json
if HStoreField:
default_mapping[HStoreField] = random_gen.gen_hstore
if CICharField:
Expand All @@ -162,16 +134,6 @@ def gen_integer():
default_mapping[CIEmailField] = random_gen.gen_email
if CITextField:
default_mapping[CITextField] = random_gen.gen_text
if AutoField:
default_mapping[AutoField] = _make_integer_gen_by_range(AutoField)
if BigAutoField:
default_mapping[BigAutoField] = _make_integer_gen_by_range(BigAutoField)
if SmallAutoField:
default_mapping[SmallAutoField] = _make_integer_gen_by_range(SmallAutoField)
if PositiveBigIntegerField:
default_mapping[PositiveBigIntegerField] = _make_integer_gen_by_range(
PositiveBigIntegerField
)
if DecimalRangeField:
default_mapping[DecimalRangeField] = random_gen.gen_pg_numbers_range(Decimal)
if IntegerRangeField:
Expand All @@ -182,8 +144,6 @@ def gen_integer():
default_mapping[DateRangeField] = random_gen.gen_date_range
if DateTimeRangeField:
default_mapping[DateTimeRangeField] = random_gen.gen_datetime_range
if NullBooleanField:
default_mapping[NullBooleanField] = random_gen.gen_boolean


# Add GIS fields
Expand Down
2 changes: 1 addition & 1 deletion model_bakery/random_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# Postgres database.
MAX_INT = 100000000000

baker_random = Random()
baker_random = Random() # noqa: S311


def get_content_file(content: bytes, name: str) -> ContentFile:
Expand Down
12 changes: 4 additions & 8 deletions model_bakery/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ def make(
_bulk_create: bool = False,
_save_kwargs: Optional[Dict[str, Any]] = None,
**attrs: Any,
) -> M:
...
) -> M: ...

@overload
def make(
Expand All @@ -108,8 +107,7 @@ def make(
_bulk_create: bool = False,
_save_kwargs: Optional[Dict[str, Any]] = None,
**attrs: Any,
) -> List[M]:
...
) -> List[M]: ...

def make(
self,
Expand Down Expand Up @@ -146,8 +144,7 @@ def prepare(
_save_related: bool = False,
_using: str = "",
**attrs: Any,
) -> M:
...
) -> M: ...

@overload
def prepare(
Expand All @@ -156,8 +153,7 @@ def prepare(
_save_related: bool = False,
_using: str = "",
**attrs: Any,
) -> List[M]:
...
) -> List[M]: ...

def prepare(
self,
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ keywords = [
classifiers = [
"Development Status :: 5 - Production/Stable",
"Framework :: Django",
"Framework :: Django :: 3.2",
"Framework :: Django :: 4.2",
"Framework :: Django :: 5.0",
"Intended Audience :: Developers",
Expand All @@ -38,7 +37,7 @@ classifiers = [
"Topic :: Software Development",
]
dependencies = [
"django>=3.2",
"django>=4.2",
]

[project.optional-dependencies]
Expand Down
15 changes: 4 additions & 11 deletions tests/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,17 @@ class Person(models.Model):
occupation = models.CharField(max_length=10, choices=OCCUPATION_CHOICES)
uuid = models.UUIDField(primary_key=False)
name_hash = models.BinaryField(max_length=16)
days_since_last_login = models.BigIntegerField()
days_since_last_login = models.SmallIntegerField()
days_since_account_creation = models.BigIntegerField()
duration_of_sleep = models.DurationField()
email = models.EmailField()
id_document = models.CharField(unique=True, max_length=10)

try:
from django.db.models import JSONField

data = JSONField()
except ImportError:
# Skip JSONField-related fields
pass
data = models.JSONField()

try:
from django.contrib.postgres.fields import (
ArrayField,
HStoreField,
JSONField as PostgresJSONField,
)
from django.contrib.postgres.fields.citext import (
CICharField,
Expand All @@ -123,7 +116,6 @@ class Person(models.Model):
if django.VERSION >= (4, 2):
long_name = models.CharField()
acquaintances = ArrayField(models.IntegerField())
postgres_data = PostgresJSONField()
hstore_data = HStoreField()
ci_char = CICharField(max_length=30)
ci_email = CIEmailField()
Expand Down Expand Up @@ -258,6 +250,7 @@ class DummyIntModel(models.Model):
class DummyPositiveIntModel(models.Model):
positive_small_int_field = models.PositiveSmallIntegerField()
positive_int_field = models.PositiveIntegerField()
positive_big_int_field = models.PositiveBigIntegerField()


class DummyNumbersModel(models.Model):
Expand Down
Loading

0 comments on commit 6d8eaee

Please sign in to comment.