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

OneToOneField #239

Merged
merged 22 commits into from
Nov 28, 2019
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ celerybeat-schedule

# dotenv
.env
.env3

# virtualenv
.venv
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Changelog
- The ``auto_now_add`` argument of ``DatetimeField`` is handled correctly in the SQLite backend.
- ``unique_together`` now creates named constrains, to prevent the DB from auto-assigning a potentially non-unique constraint name.
- Filtering by an ``auto_now`` field doesn't replace the filter value with ``now()`` anymore.
- Implemented ``OneToOneField``, one to one relation between two models.
- Prefetching is done asynchronously now, sending all prefetch request at the same time instead of in sequence.

0.15.1
------
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Contributors
* Adam Wallner ``@wallneradam``
* Zoltán Szeredi ``@zoliszeredi``
* Rebecca Klauser ``@svms1``
* Sina Sohangir ``@sinaso``

Special Thanks
==============
Expand Down
2 changes: 2 additions & 0 deletions docs/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Relational Fields
.. autoclass:: tortoise.fields.ForeignKeyField
:exclude-members: to_db_value, to_python_value

.. autoclass:: tortoise.fields.OneToOneField

.. autofunction:: tortoise.fields.ManyToManyField

.. autodata:: tortoise.fields.ForeignKeyRelation
Expand Down
23 changes: 23 additions & 0 deletions examples/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ def __str__(self):
return self.name


class Address(Model):
city = fields.CharField(max_length=64)
street = fields.CharField(max_length=128)

event: fields.OneToOneRelation[Event] = fields.OneToOneField(
"models.Event", on_delete=fields.CASCADE, related_name="address", pk=True
)

def __str__(self):
return f"Address({self.city}, {self.street})"


class Team(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
Expand All @@ -53,6 +65,9 @@ async def run():
await Event(name="Without participants", tournament_id=tournament.id).save()
event = Event(name="Test", tournament_id=tournament.id)
await event.save()

await Address.create(city="Santa Monica", street="Ocean", event=event)

participants = []
for i in range(2):
team = Team(name=f"Team {(i + 1)}")
Expand Down Expand Up @@ -96,6 +111,14 @@ async def run():

print(await Event.filter(id=event.id).values_list("id", "participants__name"))

print(await Address.filter(event=event).first())

event_reload1 = await Event.filter(id=event.id).first()
print(await event_reload1.address)

event_reload2 = await Event.filter(id=event.id).prefetch_related("address").first()
print(event_reload2.address)


if __name__ == "__main__":
run_async(run())
14 changes: 14 additions & 0 deletions tests/model_bad_rel5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
Testing Models for a bad/wrong relation reference
Wrong reference. App missing.
"""
from tortoise import fields
from tortoise.models import Model


class Tournament(Model):
id = fields.IntField(pk=True)


class Event(Model):
tournament = fields.OneToOneField("Tournament")
15 changes: 15 additions & 0 deletions tests/models_dup3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
This is the testing Models — Duplicate 3
"""

from tortoise import fields
from tortoise.models import Model


class Tournament(Model):
id = fields.IntField(pk=True)
event = fields.CharField(max_length=32)


class Event(Model):
tournament = fields.OneToOneField("models.Tournament", related_name="event")
9 changes: 9 additions & 0 deletions tests/models_o2o_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
This is the testing Models — Bad on_delete parameter
"""
from tortoise import fields
from tortoise.models import Model


class One(Model):
tournament = fields.OneToOneField("models.Two", on_delete="WABOOM")
9 changes: 9 additions & 0 deletions tests/models_o2o_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
This is the testing Models — on_delete SET_NULL without null=True
"""
from tortoise import fields
from tortoise.models import Model


class One(Model):
tournament = fields.OneToOneField("models.Two", on_delete=fields.SET_NULL)
16 changes: 16 additions & 0 deletions tests/models_schema_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ class Meta:
indexes = [("manager", "key"), ["manager_id", "name"]]


class TeamAddress(Model):
city = fields.CharField(max_length=50, description="City")
country = fields.CharField(max_length=50, description="Country")
street = fields.CharField(max_length=128, description="Street Address")
team = fields.OneToOneField(
"models.Team", related_name="address", on_delete=fields.CASCADE, pk=True
)


class VenueInformation(Model):
name = fields.CharField(max_length=128)
capacity = fields.IntField()
rent = fields.FloatField()
team = fields.OneToOneField("models.Team", on_delete=fields.SET_NULL, null=True)


class SourceFields(Model):
id = fields.IntField(pk=True, source_field="sometable_id")
chars = fields.CharField(max_length=255, source_field="some_chars_table", index=True)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_bad_relation_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,24 @@ async def test_more_than_two_dots_in_reference_init(self):
},
}
)

async def test_no_app_in_o2o_reference_init(self):
with self.assertRaisesRegex(
ConfigurationError, 'OneToOneField accepts model name in format "app.Model"'
):
await Tortoise.init(
{
"connections": {
"default": {
"engine": "tortoise.backends.sqlite",
"credentials": {"file_path": ":memory:"},
}
},
"apps": {
"models": {
"models": ["tests.model_bad_rel5"],
"default_connection": "default",
}
},
}
)
Loading