starlette_jsonapi.fields module

class starlette_jsonapi.fields.JSONAPIRelationship(id_attribute=None, related_resource=None, related_route=None, related_route_kwargs=None, *, self_route=None, self_route_kwargs=None, **kwargs)

Bases: Relationship

Mostly marshmallow_jsonapi.fields.Relationship, but friendlier with async ORMs. Accepts id_attribute which should point to the id field corresponding to a relationship object. In most cases, this attribute is available even if the relationship is not loaded.

Parameters
  • id_attribute (str) –

  • related_resource (Union[str, Type]) –

  • related_route (str) –

  • related_route_kwargs (dict) –

  • self_route (str) –

  • self_route_kwargs (dict) –

__init__(id_attribute=None, related_resource=None, related_route=None, related_route_kwargs=None, *, self_route=None, self_route_kwargs=None, **kwargs)

Serializes a related object, according to the json:api standard.

Example definition:

 1from starlette_jsonapi.schema import JSONAPISchema
 2
 3# example "models"
 4class Author:
 5    id: int
 6    name: str
 7
 8class Article:
 9    id: int
10    title: str
11    content: str
12
13    author: Author
14    author_id: int
15
16# example schemas with relationship
17class AuthorSchema(JSONAPISchema):
18    class Meta:
19        type_ = 'authors'
20
21    id = fields.Str(dump_only=True)
22    name = fields.Str(required=True)
23
24class ArticleSchema(JSONAPISchema):
25    class Meta:
26        type_ = 'articles'
27
28    id = fields.Str(dump_only=True)
29    title = fields.Str(required=True)
30    content = fields.Str(required=True)
31
32    author = JSONAPIRelationship(
33        type_='authors',
34        schema='AuthorSchema',
35        id_attribute='author_id',
36    )
Parameters
  • id_attribute (Optional[str]) – Represents the attribute name of a relationship’s id. Useful if the related object is not fetched. Otherwise, the id attribute of a related object is accessed by resolving the model attribute represented by this relationship, then accessing as related_object.id. When the related object is populated already, or when it is lazy loaded, this parameter shouldn’t be needed. It should be used when the ORM of choice is async and does not support lazy loading, or when fetching would be considered too expensive.

  • related_resource (Optional[Union[str, Type]]) – The related resource, or its name, required if you wish to enable related links inside the json:api links object.

  • related_route (Optional[str]) – The related route name, such that app.url_path_for will match back to it. Renders as related inside the links object. For example, ArticleSchema.author would specify related_route='articles:author', which would render as /articles/<id>/author.

  • related_route_kwargs (Optional[dict]) – Additional :attr:related_route kwargs that should be passed when calling app.url_path_for to build path params.

  • self_route (Optional[str]) – Same as related_route, but refers to the relationship resource GET handler. Renders as self inside the links object. For example, ArticleSchema.author would specify self_route='articles:relationships-author', which would render as /articles/<parent_id>/relationships/author.

  • self_route_kwargs (Optional[dict]) – Additional self_route kwargs that should be passed when calling app.url_path_for to build path params.

  • kwargs – Other keyword arguments passed to the base class

serialize(attr, obj, accessor=None)
get_url(obj, route_name, **kwargs)
get_self_url(obj)
property related_resource_class