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 recipes with foreign_key and one_to_one=True #205

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

### Changed
- Fix recipes declared with reverse relationship using `foreign_key` and `one_to_one=True`

### Removed

Expand All @@ -27,7 +28,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- Fix `requirements.txt` to cover Django 3.2 (everything from 2.2 till 4.0) [PR #182](https://github.com/model-bakers/model_bakery/pull/182)


## [1.3.0](https://pypi.org/project/model-bakery/1.3.0/)

### Added
Expand Down
4 changes: 4 additions & 0 deletions model_bakery/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ def _make(
"{0} iterator is empty.".format(field.name)
)

for k, v in self.model_attrs.items():
if is_iterator(v):
self.model_attrs[k] = next(v)
Comment on lines +370 to +372
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not ideal because it's bypassing all of bakery's default behavior. The proper way to do so is to make sure that self.iterator_attrs has the iterator object and the model_attrs doesn't.

From what I saw, probably the issue is under the self.get_fields methods because it's not listing the related name as a possible field for the User model.


instance = self.instance(
self.model_attrs,
_commit=commit,
Expand Down
6 changes: 6 additions & 0 deletions tests/generic/baker_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
LonelyPerson,
Person,
School,
User,
AnotherProfile,
)

person = Recipe(
Expand All @@ -35,6 +37,10 @@
name=seq("joe"),
)

profile = Recipe(AnotherProfile, email="johndoe@example.com")

user_with_profile = Recipe(User, another_profile=foreign_key(profile, one_to_one=True))

serial_numbers = Recipe(
DummyDefaultFieldsModel,
default_decimal_field=seq(Decimal("20.1")),
Expand Down
7 changes: 7 additions & 0 deletions tests/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class User(models.Model):
)


class AnotherProfile(models.Model):
user = models.OneToOneField(
User, on_delete=models.CASCADE, related_name="another_profile"
)
email = models.EmailField()


class PaymentBill(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
value = models.FloatField()
Expand Down
5 changes: 5 additions & 0 deletions tests/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ def test_one_to_one_relationship(self):
friend_ids = set([x.only_friend.id for x in lonely_people])
assert len(friend_ids) == 2

def test_reverse_one_to_one_relationship(self):
users = baker.make_recipe("tests.generic.user_with_profile", _quantity=2)
profile_ids = {u.another_profile.id for u in users}
assert len(profile_ids) == 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check here by the number of users in the database and you'll see that there are 4 of then, while you're probably expecting to only have 2.



@pytest.mark.django_db
class TestM2MField:
Expand Down