Skip to content

Commit

Permalink
Refs #416 -- Allow combination of GFK and _fill_optional
Browse files Browse the repository at this point in the history
  • Loading branch information
amureki committed Aug 9, 2024
1 parent dc54951 commit 68d2d41
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions model_bakery/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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 @@ -622,7 +622,7 @@ def _skip_field(self, field: Field) -> bool: # noqa: C901

if field.name not in self.model_attrs: # noqa: SIM102
if field.name not in self.rel_fields and (
field.null and not field.fill_optional
not field.fill_optional and field.null
):
return True

Expand Down Expand Up @@ -702,12 +702,16 @@ def generate_value(self, field: Field, commit: bool = True) -> Any: # noqa: C90
model.
"""
is_content_type_fk = False
is_generic_fk = False
if BAKER_CONTENTTYPES:
is_content_type_fk = isinstance(field, ForeignKey) and issubclass(
self._remote_field(field).model, contenttypes_models.ContentType
)
is_generic_fk = isinstance(field, GenericForeignKey)
if is_generic_fk:
generator = self.type_mapping[GenericForeignKey]
# 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:
elif field.has_default() and field.name not in self.rel_fields:
if callable(field.default):
return field.default()
return field.default
Expand Down
3 changes: 3 additions & 0 deletions model_bakery/content_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
__all__ = ["BAKER_CONTENTTYPES", "default_contenttypes_mapping"]

if BAKER_CONTENTTYPES:
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

from . import random_gen

default_contenttypes_mapping[ContentType] = random_gen.gen_content_type
# a small hack to generate random object for GenericForeignKey
default_contenttypes_mapping[GenericForeignKey] = random_gen.gen_content_type
7 changes: 7 additions & 0 deletions tests/test_filling_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ def test_iteratively_filling_generic_foreign_key_field(self):
assert dummies[1].content_type == expected_content_type
assert dummies[1].object_id == objects[1].pk

def test_with_fill_optional(self):
from django.contrib.contenttypes.models import ContentType

dummy = baker.make(models.DummyGenericForeignKeyModel, _fill_optional=True)
assert isinstance(dummy.content_type, ContentType)
assert dummy.content_type.model_class() is not None


@pytest.mark.django_db
class TestFillingForeignKeyFieldWithDefaultFunctionReturningId:
Expand Down

0 comments on commit 68d2d41

Please sign in to comment.