Skip to content

Commit

Permalink
Allow None value for generic foreign keys within iterators (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-engel authored Aug 20, 2024
1 parent 3a65fa5 commit 0123af2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions model_bakery/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,10 @@ def _handle_generic_foreign_keys(self, instance: Model, attrs: Dict[str, Any]):
content_type_field = data["content_type_field"]
object_id_field = data["object_id_field"]
value = data["value"]
if value is None:
continue
if is_iterator(value):
value = next(value)
if value is None:
continue

setattr(instance, field_name, value)
setattr(
Expand Down
18 changes: 18 additions & 0 deletions tests/test_filling_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,24 @@ def test_with_iter(self):
assert dummies[1].content_type == expected_content_type
assert dummies[1].object_id == objects[1].pk

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

profile = baker.make(models.Profile)
dummies = baker.make(
models.DummyGenericForeignKeyModel,
content_object=iter((None, profile)),
_quantity=2,
)

expected_content_type = ContentType.objects.get_for_model(models.Profile)

assert dummies[0].content_object is None

assert dummies[1].content_object == profile
assert dummies[1].content_type == expected_content_type
assert dummies[1].object_id == profile.pk

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

Expand Down

0 comments on commit 0123af2

Please sign in to comment.