Closed falloutphil closed 1 year ago
Quick additon - #292 seems to ask the same question, and the response seems to line up with my final suggestion of using fields.Nested
as a workaround (but without the conditional test which is wrong). Also it looks like I've misunderstood the original justification which is to set include_relationships
to False
, specifically to avoid it duplicating the values already providied by include_fk
? It was never designed to include auto generated nested schemas in the dumped JSON, nor is such a thing possible currently by any other means. This is covered fairly conclusively in #98, I think.
Unless anyone comments otherwise (i.e. my summary above is wrong!) I'll close this shortly - I note in the issues above at least a couple of other people seem to have made similar incorrect assumptions to me. It might be worth making the context and purpose of include_relationships
setting a bit more explicit in the otherwise good documentation on the library, in case a few other people have fallen into the same trap as me! I can raise another ticket for that if people think it is worthwhile?
Hi,
Requirements for reference:
If I have a model with a foreign key, my expectation was that
include_relationships
would mean that any serialization taking place on an instance of my model would nest the model referenced by the foreign key.However after running some basic examples that doesn't seem to be happening - so my question is what exactly does
include_relationships
do? In my experiments it seems to behave almost identically toinclude_fk
?Some details below on what I tried. Perhaps there is some weird interaction with my methodology, or perhaps I've just misunderstood the parameter? Can anyone shed any light?
Thanks!
Helper class so I can set include_relationships everywhere at once (problem occurs without using this but provided for context):
Simple as possible schema:
Model referenced in the Schema - contains one foreign key:
Model referenced by the foreign key which isn't serialised in the output below.
For some context this is how I serialise the object using
dump()
:In the helper class (or setting it directly under
QuoteSchema
'sMeta
- it makes no difference), settinginclude_relationships=True
results in the following dump - notice user is not showing nested objects, instead it is just setting the the foreign key id representing the nested object.I kinda expected that include_relationships would toggle if the referenced model was nested in the serialized object?
Set
include_relationships=False
results in the following dump - this is as expected; onlyQuote
's fields are in the output:However if also set include_fk, I just get the
user_id
key which duplicates to the value of theuser
key:Finally note I am able to get more or less the behaviour I thought I'd get for free using this in my
QuoteSchema
, but it seems like I might be re-inventing the wheel?