From f62f63f2a59b786db09b36fde7eb924b11d1850e Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 20 Jan 2020 13:05:40 -0300 Subject: [PATCH 1/9] Add custom field generator to conftest to force AppRegistryNotReady exception --- tests/conftest.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index ec871e1a..cc423046 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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, @@ -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() From 23fb9cd9d2418390addeb5064278a0632e6e1d7c Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 20 Jan 2020 13:11:15 -0300 Subject: [PATCH 2/9] ContentType db objects should be referenced during execution time --- model_bakery/baker.py | 9 +++++---- model_bakery/generators.py | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/model_bakery/baker.py b/model_bakery/baker.py index 9f4f149d..6c9d3d73 100644 --- a/model_bakery/baker.py +++ b/model_bakery/baker.py @@ -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 ( @@ -404,6 +403,8 @@ def is_rel_field(x): def _skip_field(self, field): # check for fill optional argument + from django.contrib.contenttypes.fields import GenericRelation + if isinstance(self.fill_in_optional, bool): field.fill_optional = self.fill_in_optional else: @@ -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 ): - 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: diff --git a/model_bakery/generators.py b/model_bakery/generators.py index dc1247ae..101ff7c7 100644 --- a/model_bakery/generators.py +++ b/model_bakery/generators.py @@ -1,4 +1,3 @@ -from django.contrib.contenttypes.models import ContentType from django.db.models import ( BigIntegerField, BinaryField, @@ -88,7 +87,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: @@ -109,7 +107,9 @@ def get_type_mapping(): + from django.contrib.contenttypes.models import ContentType mapping = default_mapping.copy() + mapping[ContentType] = random_gen.gen_content_type return mapping.copy() From efb416511570c5252a95e2aef737ebfdb9eae5c6 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 20 Jan 2020 13:11:46 -0300 Subject: [PATCH 3/9] GIS db objects must be imported during execution time --- model_bakery/generators.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/model_bakery/generators.py b/model_bakery/generators.py index 101ff7c7..6b3b7528 100644 --- a/model_bakery/generators.py +++ b/model_bakery/generators.py @@ -29,7 +29,6 @@ ) from . import random_gen -from .gis import default_gis_mapping from .utils import import_from_str try: @@ -103,13 +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() From 7ab87a228bf6fa0c12068032abcd42a4abeaff6b Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 20 Jan 2020 13:21:07 -0300 Subject: [PATCH 4/9] Import should be placed before line comment --- model_bakery/baker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model_bakery/baker.py b/model_bakery/baker.py index 6c9d3d73..5b85734f 100644 --- a/model_bakery/baker.py +++ b/model_bakery/baker.py @@ -402,9 +402,9 @@ def is_rel_field(x): ] def _skip_field(self, field): - # check for fill optional argument 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 else: From 9deda04f600f91c0f97b789500195584da6932ce Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Tue, 21 Jan 2020 12:23:28 -0300 Subject: [PATCH 5/9] Update Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1696e4b6..4ffd1526 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Add black job (https://github.com/model-bakers/model_bakery/pull/42) - README.md instead of rst (https://github.com/model-bakers/model_bakery/pull/44) - Add Django 3.0 and Python 3.8 to CI (https://github.com/model-bakers/model_bakery/pull/48/) +- Fixes bug when registerin custom field's generator via `settings.py` ### Removed From 6c904326dde88b97414c4971f312d23c4b6570fe Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Tue, 21 Jan 2020 12:26:35 -0300 Subject: [PATCH 6/9] Create boolean flag to be more explicit with the if statement --- model_bakery/baker.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/model_bakery/baker.py b/model_bakery/baker.py index 5b85734f..7c618e64 100644 --- a/model_bakery/baker.py +++ b/model_bakery/baker.py @@ -489,13 +489,15 @@ def generate_value(self, field, commit=True): `attr_mapping` and `type_mapping` can be defined easily overwriting the model. """ + is_content_type_fk = isinstance(field, ForeignKey) and issubclass( + self._remote_field(field).model, contenttypes.models.ContentType + ) + if field.name in self.attr_mapping: generator = self.attr_mapping[field.name] elif getattr(field, "choices"): generator = random_gen.gen_from_choices(field.choices) - elif isinstance(field, ForeignKey) and issubclass( - self._remote_field(field).model, contenttypes.models.ContentType - ): + elif is_content_type_fk: generator = self.type_mapping[contenttypes.models.ContentType] elif generators.get(field.__class__): generator = generators.get(field.__class__) From 1ff384a9dd87c6d44ea8f41dfefdaee95da46bae Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Tue, 21 Jan 2020 12:43:45 -0300 Subject: [PATCH 7/9] Add unit test to ensure custom field config works via django settings --- tests/conftest.py | 4 +++- tests/generic/fields.py | 4 ++++ tests/generic/models.py | 5 +++++ tests/test_filling_fields.py | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index cc423046..c58162dd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -41,6 +41,8 @@ def pytest_configure(): from model_bakery import baker - baker.generators.add("tests.generic.fields.CustomFieldWithGenerator", None) + def gen_same_text(): + return 'always the same text' + baker.generators.add("tests.generic.fields.CustomFieldViaSettings", gen_same_text) django.setup() diff --git a/tests/generic/fields.py b/tests/generic/fields.py index d76eae84..10b7bd43 100644 --- a/tests/generic/fields.py +++ b/tests/generic/fields.py @@ -9,6 +9,10 @@ class CustomFieldWithoutGenerator(models.TextField): pass +class CustomFieldViaSettings(models.TextField): + pass + + class FakeListField(models.TextField): def to_python(self, value): return value.split() diff --git a/tests/generic/models.py b/tests/generic/models.py index 58573b45..9c3a5cb3 100755 --- a/tests/generic/models.py +++ b/tests/generic/models.py @@ -16,6 +16,7 @@ from .fields import ( CustomFieldWithGenerator, CustomFieldWithoutGenerator, + CustomFieldViaSettings, FakeListField, CustomForeignKey, ) @@ -291,6 +292,10 @@ class CustomFieldWithoutGeneratorModel(models.Model): custom_value = CustomFieldWithoutGenerator() +class CustomFieldViaSettingsModel(models.Model): + custom_value = CustomFieldViaSettings() + + class CustomForeignKeyWithGeneratorModel(models.Model): custom_fk = CustomForeignKey( Profile, blank=True, null=True, on_delete=models.CASCADE diff --git a/tests/test_filling_fields.py b/tests/test_filling_fields.py index 7013460d..fe45c648 100644 --- a/tests/test_filling_fields.py +++ b/tests/test_filling_fields.py @@ -334,6 +334,10 @@ def gen_char(): assert "Some value" == person.name + def test_ensure_adding_generators_via_settings_works(self): + obj = baker.make(models.CustomFieldViaSettingsModel) + assert "always the same text" == obj.custom_value + @pytest.mark.django_db class TestFillingAutoFields: From 4c29a4a467fcbabc99b0bed26db15956bc530201 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Tue, 21 Jan 2020 13:00:48 -0300 Subject: [PATCH 8/9] Update CHANGELOG.md Co-Authored-By: Ana Paula Gomes --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ffd1526..6d8a7e3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Add black job (https://github.com/model-bakers/model_bakery/pull/42) - README.md instead of rst (https://github.com/model-bakers/model_bakery/pull/44) - Add Django 3.0 and Python 3.8 to CI (https://github.com/model-bakers/model_bakery/pull/48/) -- Fixes bug when registerin custom field's generator via `settings.py` +- Fixes bug when registering custom fields generator via `settings.py` ### Removed From 91047893169690e3ab4b03354e1379ad18ecc7b9 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Tue, 21 Jan 2020 13:46:24 -0300 Subject: [PATCH 9/9] Fix black warning --- tests/conftest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index c58162dd..b36d4791 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -42,7 +42,8 @@ def pytest_configure(): from model_bakery import baker def gen_same_text(): - return 'always the same text' + return "always the same text" + baker.generators.add("tests.generic.fields.CustomFieldViaSettings", gen_same_text) django.setup()