You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
This problem just came up for me. I've got models that represent a person, and the person can have one or more "tickets", and each ticket may have an associated "pass" (think like going to a convention where the pass is the thing around your neck, the ticket is a more abstract thing you buy). Python veterans will immediately spot the problem: pass is a reserved word. Simplified code:
frommarshmallow_jsonapi.flaskimportRelationship, SchemaclassPerson(Schema):
... otherpersonfields ...
tickets=Relationship(many=True, schema=Ticket, ...)
classTicket(Schema):
... otherticketfields ...
# because it's a reserved word, this field is named pass_ but we set the data_key so it shows up as passpass_=Relationship(schema=Pass, data_key='pass')
classPass(Schema):
... passfields ...
The problem comes when trying to include the pass relationship, e.g. Person(include_data=['tickets.pass']) generates an error
marshmallow_jsonapi/schema.py, line 78, in __init__
self.check_relations(self.include_data)",
marshmallow_jsonapi/schema.py, line 117, in check_relations
field.schema.check_relations(fields[1:])
marshmallow_jsonapi/schema.py, line 105, in check_relations
raise ValueError(f'Unknown field "{local_field}"')
ValueError: Unknown field "pass"
This happens because this code looks in fields and pass isn't there (in fields it's named pass_)
local_field = fields[0]
if local_field not in self.fields:
raise ValueError(f'Unknown field "{local_field}"')
Replacing that first line with
local_field = next((n for n, f in self.fields.items() if f.data_key == fields[0]), fields[0])
seems to work--that just says use a field whose data_key matches the value from the include (if there is one). I'm guessing the vast majority of the time the field and the data_key are the same so this doesn't matter, but shouldn't includes always use the data_key since that's the only name the rest of the world knows the relationship by?
The text was updated successfully, but these errors were encountered:
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
This problem just came up for me. I've got models that represent a person, and the person can have one or more "tickets", and each ticket may have an associated "pass" (think like going to a convention where the pass is the thing around your neck, the ticket is a more abstract thing you buy). Python veterans will immediately spot the problem:
pass
is a reserved word. Simplified code:The problem comes when trying to include the
pass
relationship, e.g.Person(include_data=['tickets.pass'])
generates an errorThis happens because this code looks in fields and
pass
isn't there (in fields it's namedpass_
)Replacing that first line with
seems to work--that just says use a field whose data_key matches the value from the include (if there is one). I'm guessing the vast majority of the time the field and the data_key are the same so this doesn't matter, but shouldn't includes always use the data_key since that's the only name the rest of the world knows the relationship by?
The text was updated successfully, but these errors were encountered: