Skip to content

Use related_name for MTO queries #123

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

Merged
merged 3 commits into from
Jan 14, 2023
Merged
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -22,7 +22,9 @@ Using the following categories, list your changes in this order:

## [Unreleased]

- Nothing (yet)
### Fixed

- `use_query` will now utilize `field.related_name` when postprocessing many-to-one relationships

## [2.2.1] - 2022-01-09

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ ensure_newline_before_comments = "True"
include_trailing_comma = "True"
line_length = 88
lines_after_imports = 2
extend_skip_glob = ["*/migrations/*"]

[tool.mypy]
ignore_missing_imports = true
2 changes: 1 addition & 1 deletion src/django_idom/utils.py
Original file line number Diff line number Diff line change
@@ -232,7 +232,7 @@ def django_query_postprocessor(
getattr(data, field.name)

if many_to_one and type(field) == ManyToOneRel:
prefetch_fields.append(f"{field.name}_set")
prefetch_fields.append(field.related_name or f"{field.name}_set")

elif many_to_many and isinstance(field, ManyToManyField):
prefetch_fields.append(field.name)
2 changes: 1 addition & 1 deletion tests/test_app/components.py
Original file line number Diff line number Diff line change
@@ -199,7 +199,7 @@ def relational_query():

mtm = relational_parent.data.many_to_many.all()
oto = relational_parent.data.one_to_one
mto = relational_parent.data.foriegnchild_set.all()
mto = relational_parent.data.many_to_one.all()
fk = foriegn_child.data.parent

return html.div(
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.1.5 on 2023-01-13 23:23

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("test_app", "0002_relationalchild_relationalparent_foriegnchild"),
]

operations = [
migrations.AlterField(
model_name="foriegnchild",
name="parent",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="parent",
to="test_app.relationalparent",
),
),
migrations.AlterField(
model_name="relationalparent",
name="one_to_one",
field=models.OneToOneField(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="one_to_one",
to="test_app.relationalchild",
),
),
]
23 changes: 23 additions & 0 deletions tests/test_app/migrations/0004_alter_foriegnchild_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.1.5 on 2023-01-13 23:28

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("test_app", "0003_alter_foriegnchild_parent_and_more"),
]

operations = [
migrations.AlterField(
model_name="foriegnchild",
name="parent",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="foriegn_child",
to="test_app.relationalparent",
),
),
]
23 changes: 23 additions & 0 deletions tests/test_app/migrations/0005_alter_foriegnchild_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.1.5 on 2023-01-13 23:38

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("test_app", "0004_alter_foriegnchild_parent"),
]

operations = [
migrations.AlterField(
model_name="foriegnchild",
name="parent",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="many_to_one",
to="test_app.relationalparent",
),
),
]
2 changes: 1 addition & 1 deletion tests/test_app/models.py
Original file line number Diff line number Diff line change
@@ -20,4 +20,4 @@ class RelationalParent(models.Model):

class ForiegnChild(models.Model):
text = models.CharField(max_length=1000) # type: ignore
parent = models.ForeignKey(RelationalParent, on_delete=models.CASCADE) # type: ignore
parent = models.ForeignKey(RelationalParent, related_name="many_to_one", on_delete=models.CASCADE) # type: ignore
2 changes: 1 addition & 1 deletion tests/test_app/tests/test_components.py
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ def setUpClass(cls):
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

# FIXME: This is required otherwise the tests will throw a `SynchronousOnlyOperation`
# error when deleting the test datatabase. Potentially a Django bug.
# error when discarding the test datatabase. Potentially a `ChannelsLiveServerTestCase` bug.
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"

super().setUpClass()