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

Reproduce: Cannot call baker.make() with ForeignKey default=pk, only actual model instance #137

Closed
17 changes: 17 additions & 0 deletions tests/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ class DummyGenericRelationModel(models.Model):
relation = GenericRelation(DummyGenericForeignKeyModel)


class NamedThing(models.Model):
name = models.CharField(max_length=64)


def get_default_namedthing_id():
instance, _ = NamedThing.objects.get_or_create(name="Default")
return instance.id


class DummyForeignKeyWithDefaultIdModel(models.Model):
named_thing = models.ForeignKey(
"NamedThing",
default=get_default_namedthing_id,
on_delete=models.SET_DEFAULT,
)


class DummyNullFieldsModel(models.Model):
null_foreign_key = models.ForeignKey(
"DummyBlankFieldsModel", null=True, on_delete=models.CASCADE
Expand Down
9 changes: 9 additions & 0 deletions tests/test_filling_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ def test_filling_content_type_field(self):
assert dummy.content_type.model_class() is not None


@pytest.mark.django_db
class TestFillingForeignKeyFieldWithDefaultFunctionReturningId:
def test_filling_foreignkey_with_default_id(self):
dummy = baker.make(
models.DummyForeignKeyWithDefaultIdModel, named_thing__name="Default"
)
assert dummy.named_thing.id == models.get_default_namedthing_id()


@pytest.mark.django_db
class TestsFillingFileField:
def test_filling_file_field(self):
Expand Down