Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when adding custom fields generators to bakery via settings file #58

Merged
merged 10 commits into from
Jan 22, 2020
9 changes: 5 additions & 4 deletions model_bakery/baker.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from os.path import dirname, join

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib import contenttypes
from django.apps import apps
from django.contrib.contenttypes.fields import GenericRelation

from django.db.models.base import ModelBase
from django.db.models import (
Expand Down Expand Up @@ -403,6 +402,8 @@ def is_rel_field(x):
]

def _skip_field(self, field):
from django.contrib.contenttypes.fields import GenericRelation

# check for fill optional argument
if isinstance(self.fill_in_optional, bool):
field.fill_optional = self.fill_in_optional
Expand Down Expand Up @@ -493,9 +494,9 @@ def generate_value(self, field, commit=True):
elif getattr(field, "choices"):
generator = random_gen.gen_from_choices(field.choices)
elif isinstance(field, ForeignKey) and issubclass(
self._remote_field(field).model, ContentType
self._remote_field(field).model, contenttypes.models.ContentType
berinhard marked this conversation as resolved.
Show resolved Hide resolved
):
generator = self.type_mapping[ContentType]
generator = self.type_mapping[contenttypes.models.ContentType]
elif generators.get(field.__class__):
generator = generators.get(field.__class__)
elif field.__class__ in self.type_mapping:
Expand Down
10 changes: 6 additions & 4 deletions model_bakery/generators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import (
BigIntegerField,
BinaryField,
Expand Down Expand Up @@ -30,7 +29,6 @@
)

from . import random_gen
from .gis import default_gis_mapping
from .utils import import_from_str

try:
Expand Down Expand Up @@ -88,7 +86,6 @@
FileField: random_gen.gen_file_field,
ImageField: random_gen.gen_image_field,
DurationField: random_gen.gen_interval,
ContentType: random_gen.gen_content_type,
}

if ArrayField:
Expand All @@ -105,11 +102,16 @@
default_mapping[CITextField] = random_gen.gen_text

# Add GIS fields
default_mapping.update(default_gis_mapping)


def get_type_mapping():
from django.contrib.contenttypes.models import ContentType
from .gis import default_gis_mapping

mapping = default_mapping.copy()
mapping[ContentType] = random_gen.gen_content_type
default_mapping.update(default_gis_mapping)

return mapping.copy()


Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def pytest_configure():
] + installed_apps
else:
raise NotImplementedError("Tests for % are not supported", test_db)

settings.configure(
DATABASES={"default": {"ENGINE": db_engine, "NAME": db_name}},
INSTALLED_APPS=installed_apps,
Expand All @@ -37,4 +38,9 @@ def pytest_configure():
MIDDLEWARE=(),
USE_TZ=os.environ.get("USE_TZ", False),
)

from model_bakery import baker

baker.generators.add("tests.generic.fields.CustomFieldWithGenerator", None)

django.setup()