Skip to content

Commit

Permalink
Fix #483 -- Add Django 5.1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
amureki committed Aug 8, 2024
1 parent dc54951 commit 6b66ff2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 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
- Add Django 5.1 support

### Changed

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

from django import VERSION as DJANGO_VERSION
from django.apps import apps
from django.conf import settings
from django.db.models import (
Expand Down Expand Up @@ -51,7 +52,7 @@

if BAKER_CONTENTTYPES:
from django.contrib.contenttypes import models as contenttypes_models
from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
else:
contenttypes_models = None
GenericRelation = None
Expand Down Expand Up @@ -703,9 +704,14 @@ def generate_value(self, field: Field, commit: bool = True) -> Any: # noqa: C90
"""
is_content_type_fk = False
if BAKER_CONTENTTYPES:
is_content_type_fk = isinstance(field, ForeignKey) and issubclass(
self._remote_field(field).model, contenttypes_models.ContentType
)
if DJANGO_VERSION >= (5, 1):
# GenericForeignKey became a proper field-citizen.
# See https://github.com/django/django/commit/6002df06713cb0a7050432263527a25754190c27
is_content_type_fk = isinstance(field, GenericForeignKey)
else:
is_content_type_fk = isinstance(field, ForeignKey) and issubclass(
self._remote_field(field).model, contenttypes_models.ContentType
)
# we only use default unless the field is overwritten in `self.rel_fields`
if field.has_default() and field.name not in self.rel_fields:
if callable(field.default):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ classifiers = [
"Framework :: Django",
"Framework :: Django :: 4.2",
"Framework :: Django :: 5.0",
"Framework :: Django :: 5.1",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
Expand Down
7 changes: 4 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[tox]
env_list =
py{38,39}-django{42}-{postgresql,sqlite}
py{310,311}-django{42,50}-{postgresql,sqlite}
py{311,312}-django{42,50}-{postgresql-psycopg3}
py312-django50-{postgresql-contenttypes}
py{310,311}-django{42,50,51}-{postgresql,sqlite}
py{311,312}-django{42,50,51}-{postgresql-psycopg3}
py312-django{50,51}-{postgresql-contenttypes}

[testenv]
package = wheel
Expand All @@ -25,6 +25,7 @@ deps =
pytest-django
django42: Django>=4.2,<5
django50: Django>=5.0,<5.1
django51: Django>=5.1,<5.2
postgresql: psycopg2-binary
postgresql-psycopg3: psycopg
commands =
Expand Down

0 comments on commit 6b66ff2

Please sign in to comment.