marshmallow-jsonapi
marshmallow-jsonapi copied to clipboard
self_url from Meta with <attribute.id> raises error
Hi all,
Why am I not able to refer an attribute from a relationship in the self_url for a specific schema but I can in a Relationship.
from marshmallow_jsonapi import Schema, fields
class User:
def __init__(self, id, name):
self.id = id
self.name = name
class Comment:
def __init__(self, id, body, author):
self.id = id
self.body = body
self.author = author
class Post:
def __init__(self, id, title, author, comments=None):
self.id = id
self.title = title
self.author = author # User object
self.comments = [] if comments is None else comments # Comment objects
class PostSchema(Schema):
id = fields.Str(dump_only=True)
title = fields.Str()
author = fields.Relationship(
self_url='/posts/{post_id}/relationships/author',
self_url_kwargs={'post_id': '<id>'},
related_url='/authors/{author_id}',
related_url_kwargs={'author_id': '<author.id>'},
include_resource_linkage=True,
type_='users'
)
class Meta:
type_ = 'posts'
strict = True
self_url = '/author/{author_id}/posts/{id}/'
self_url_kwargs = {'id': '<id>', 'author_id': '<author.id>'}
self_url_many = '/posts/'
class UserSchema(Schema):
id = fields.Str(dump_only=True)
name = fields.Str()
class Meta:
type_ = 'users'
strict = True
user = User(id='94', name='Laura')
post = Post(id='1', title='Django is Omakase', author=user)
PostSchema().dump(post)
Here is my example where PostSchema has self_url = '/author/{author_id}/posts/{id}/' and it raises the error below. This is not happening when I specify related_url='/authors/{author_id}' on the relationship
Traceback (most recent call last):
File "C:/Users/Joan/.PyCharmCE2019.1/config/scratches/scratch_6.py", line 50, in <module>
PostSchema().dump(post)
File "D:\Users\Joan\venvs\kii\lib\site-packages\marshmallow\schema.py", line 544, in dump
original_data=obj)
File "D:\Users\Joan\venvs\kii\lib\site-packages\marshmallow\schema.py", line 875, in _invoke_dump_processors
data=data, many=many, original_data=original_data)
File "D:\Users\Joan\venvs\kii\lib\site-packages\marshmallow\schema.py", line 976, in _invoke_processors
data = utils.if_none(processor(data, many), data)
File "D:\Users\Joan\venvs\kii\lib\site-packages\marshmallow_jsonapi\schema.py", line 128, in format_json_api_response
ret = self.format_items(data, many)
File "D:\Users\Joan\venvs\kii\lib\site-packages\marshmallow_jsonapi\schema.py", line 389, in format_items
return self.format_item(data)
File "D:\Users\Joan\venvs\kii\lib\site-packages\marshmallow_jsonapi\schema.py", line 376, in format_item
links = self.get_resource_links(item)
File "D:\Users\Joan\venvs\kii\lib\site-packages\marshmallow_jsonapi\schema.py", line 408, in get_resource_links
kwargs = resolve_params(item, self.opts.self_url_kwargs or {})
File "D:\Users\Joan\venvs\kii\lib\site-packages\marshmallow_jsonapi\utils.py", line 52, in resolve_params
'attribute of {obj!r}'.format(attr_name=attr_name, obj=obj),
AttributeError: 'author.id' is not a valid attribute of {'id': '1', 'author': {'links': {'self': '/posts/1/relationships/author', 'related': '/authors/94'}, 'data': {'type': 'users', 'id': '94'}}, 'title': 'Django is Omakase'}
Thank you for your time.