Skip to content

Commit

Permalink
Conditionally support NullBooleanField
Browse files Browse the repository at this point in the history
  • Loading branch information
ashiazed committed Oct 26, 2021
1 parent f278865 commit 88318e3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

### Changed
- Conditionally support NullBooleanField, it's under deprecation and will be removed in Django 4.0 [PR #25](https://github.com/model-bakers/model_bakery/pull/250)

### Removed

Expand Down
11 changes: 9 additions & 2 deletions model_bakery/generators.py
Expand Up @@ -20,7 +20,6 @@
IntegerField,
IPAddressField,
ManyToManyField,
NullBooleanField,
OneToOneField,
PositiveIntegerField,
PositiveSmallIntegerField,
Expand Down Expand Up @@ -87,6 +86,13 @@
CIEmailField = None
CITextField = None

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


try:
# PostgreSQL-specific fields (only available when psycopg2 is installed)
from django.contrib.postgres.fields.ranges import (
Expand Down Expand Up @@ -124,7 +130,6 @@ def gen_integer():
OneToOneField: random_gen.gen_related,
ManyToManyField: random_gen.gen_m2m,
BooleanField: random_gen.gen_boolean,
NullBooleanField: random_gen.gen_boolean,
IntegerField: _make_integer_gen_by_range(IntegerField),
BigIntegerField: _make_integer_gen_by_range(BigIntegerField),
SmallIntegerField: _make_integer_gen_by_range(SmallIntegerField),
Expand Down Expand Up @@ -185,6 +190,8 @@ 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 tests/generic/models.py
Expand Up @@ -228,7 +228,7 @@ def save(self, *args, **kwargs):

class Classroom(models.Model):
students = models.ManyToManyField(Person, null=True)
active = models.NullBooleanField()
active = models.BooleanField(null=True)


class Store(models.Model):
Expand Down

0 comments on commit 88318e3

Please sign in to comment.