Skip to content

Commit

Permalink
Fixed prefetching over OneToOne relations (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
grigi committed Apr 26, 2020
1 parent bad2710 commit d1c9117
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Changelog
- Pydantic models with a Primary Key that is also a raw field of a relation is now not hidden when ``exclude_raw_fields=True`` as it is a critically important field
- Raise an informative error when a field is set as nullable and primary key at the same time
- Foreign key id's are now described to have the positive-integer range of the field it is related to
- Fixed prefetching over OneToOne relations

0.16.8
------
Expand Down
34 changes: 31 additions & 3 deletions tests/contrib/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,11 +787,39 @@ async def test_event(self):
},
)

@test.skip("Temp skip")
async def test_address(self):
addressp = await self.Address_Pydantic.from_tortoise_orm(await Address.get(street="Ocean"))
print(addressp.json(indent=4))
# addressdict = addressp.dict()
# print(addressp.json(indent=4))
addressdict = addressp.dict()

# Remove timestamps
del addressdict["event"]["tournament"]["created"]
del addressdict["event"]["modified"]

self.assertEqual(
addressdict,
{
"city": "Santa Monica",
"street": "Ocean",
"event": {
"event_id": self.event.event_id,
"name": "Test",
"tournament": {
"id": self.tournament.id,
"name": "New Tournament",
"desc": None,
},
"reporter": {"id": self.reporter.id, "name": "The Reporter"},
"participants": [
{"id": self.team1.id, "name": "Onesies", "alias": None},
{"id": self.team2.id, "name": "T-Shirts", "alias": None},
],
"token": self.event.token,
"alias": None,
},
"event_id": self.address.event_id,
},
)

async def test_tournament(self):
tournamentp = await self.Tournament_Pydantic.from_tortoise_orm(
Expand Down
1 change: 1 addition & 0 deletions tortoise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def split_reference(reference: str) -> Tuple[str, str]:
else:
key_o2o_object = deepcopy(related_model._meta.pk)
o2o_object.to_field_instance = related_model._meta.pk
o2o_object.to_field = related_model._meta.pk_attr

key_field = f"{field}_id"
key_o2o_object.pk = o2o_object.pk
Expand Down

0 comments on commit d1c9117

Please sign in to comment.