How to skip an intermediary model? #97
-
Hi Folks. I've got three models I'm serialising The relationship would be I don't suppose you have an example of this do you? Thanks 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
OK, so I've got this working but currently it's a little long-winded. Here's a diff to an example from diff --git a/tests/test_pairs.py b/tests/test_pairs.py
index 0c9bbfb..5db53d2 100644
--- a/tests/test_pairs.py
+++ b/tests/test_pairs.py
@@ -537,10 +537,18 @@ class PairsTestCase(TestCase):
)
def test_select_related(self):
- owner = Owner.objects.create(name="test owner")
+ group = Group.objects.create(name="test group")
+ owner = Owner.objects.create(name="test owner", group=group)
Widget.objects.create(name="test widget", owner=owner)
prepare, project = pairs.combine(
+ (
+ qs.pipe(
+ qs.select_related("owner__group"),
+ qs.include_fields("owner__group__name"),
+ ),
+ projectors.noop,
+ ),
(
qs.pipe(
qs.select_related("owner"),
@@ -561,6 +569,21 @@ class PairsTestCase(TestCase):
),
),
),
+ (
+ qs.noop,
+ projectors.producer_to_projector(
+ "owner_group",
+ producers.relationship(
+ "owner",
+ producers.relationship(
+ "group",
+ projectors.producer_to_projector(
+ "name", producers.attr("name")
+ ),
+ ),
+ ),
+ ),
+ ),
)
with self.assertNumQueries(0):
@@ -573,7 +596,11 @@ class PairsTestCase(TestCase):
result = project(instance)
self.assertEqual(
- result, {"name": "test widget", "owner": {"name": "test owner"}}
+ result, {
+ "name": "test widget",
+ "owner": {"name": "test owner"},
+ 'owner_group': {'name': 'test group'},
+ }
) From the Having the nested The pattern otherwise makes sense, but it takes some thinking about. (That might just be How it is™) @j4mie, could I ask, do you have any suggestions of how I can do this better? 🎁 |
Beta Was this translation helpful? Give feedback.
-
Hi Carlton! First, apologies for missing this originally. I got the notification when you tagged me, but not for the original discussion. Will check my settings. I'll give this some proper thought later, but as an initial comment I thought it'd be useful to share an excerpt from our internal best practices document that seems relevant here. I'd love your take on it. Note this is mostly oriented around developers building backend endpoints to support frontend features in a React app, rather than building a public API - is that what you're trying to do?
|
Beta Was this translation helpful? Give feedback.
-
Not really an answer as such, but here's a slightly higher-level way to achieve the thing you were trying to do: spec = [
...
{
"owner_group": (
pairs.discard_projector(
specs.relationship(
"owner",
[{"group": ["name"]}],
to_attr="owner_for_owner_group",
)
),
projectors.producer_to_projector(
"name", producers.attr("owner_for_owner_group.group.name")
)
)
},
...
]
You could also just substitute the second part of the pair with If we wanted to genericise this somehow and add it to the library, we'd have to consider:
Still pondering... |
Beta Was this translation helpful? Give feedback.
Not really an answer as such, but here's a slightly higher-level way to achieve the thing you were trying to do:
to_attr
so we don't …