Releases: Vacasa/drf-jsonapi
Releases · Vacasa/drf-jsonapi
1.0.3
0.5.4
1.0.2
1.0.1
0.5.3
0.5.2
1.0.0
Included in this Release:
1.0rc #45 - @chrisbrantley
DRF-JSONAPI 1.0 Migration Guide
For migrating your drf-jsonapi
project from <= 0.5.1 to >= 1.0.0. This guide includes some helpful information & examples for navigating breaking changes but is by no means exhaustive.
Serializers
- Replace the meta
base_path
attribute withbasename
. Leading forward slashes are no longer needed. - Remove
relationships()
meta class attribute and add a static method calleddefine_relationships
on the serializer. This method should return a dictionary of relationship handler objects
# Before
from .relationships import OwnerRelationshipHandler
class CarSerializer(ResourceModelSerializer):
class Meta:
type = "car"
base_path = "/cars"
model = Car
fields = ("make", "model", "year")
relationships = {
"owners": OwnerRelationshipHandler()
}
# After
from drf_jsonapi.relationships import RelationshipHandler
from api.owners.serializers import OwnerSerializer
class CarSerializer(ResourceModelSerializer):
class Meta:
type = "car"
basename = "cars"
model = Car
fields = ("make", "model", "year")
@staticmethod
def define_relationships():
return {
"owners": RelationshipHandler(OwnerSerializer, many=True)
}
URLs
RelationshipRouter
has been removed and is no longer necessary. Set your relationships in your serializer viadefine_relationships()
and use the newRouter
class.- The relationship segment of a relationship url path can be specified by passing
url_segment
to you relationship handler. See below for more detail.
# Before
from rest_framework_nested.routers import DefaultRouter
from drf_jsonapi.routers import RelationshipRouter
from api.views import CarViewSet, OwnerRelationshipViewSet
from api.urls import urlpatterns
router = DefaultRouter(trailing_slash=False)
router.register(^"/cars", CarViewSet, basename="cars")
owner_router = RelationshipRouter(
router, r"owners", lookup="owners", trailing_slash=False
)
owner_router.register(
r"relationships/owners", views.OwnerRelationshipViewSet, base_name="owners"
)
urlpatterns += router.urls + owner_router.urls
# After
from drf_jsonapi.routers import Router
from api.views import CarViewSet
router = Router(trailing_slash=False)
router.register(CarViewSet)
urlpatterns += router.urls
Relationships
RelationshipHandler
classes now have__init__
arguments.- The handler's serializer class must now be passed as the first argument to a
RelationshipHandler
instance. This value may be a serializer class or a module string that resolves to a serializer class. - The dictionary key of a serializer's
define_relationships()
method will determine the default segment name used for relationship urls. This functionality can be overwritten by passing theurl_segment
kwarg to the handler on initialization.
# Given the following handler, the cars relationship GET would look like this: /cars/:id/relatonships/owners
handler = RelationshipHandler(
OwnersSerializer,
many=True,
related_field="owners",
)
# If the desired endpoint was instead: /cars/:id/relatonships/car-owners
# Modify the handler with a `url_segment` kwarg like shown below.
handler = RelationshipHandler(
OwnersSerializer,
many=True,
related_field="owners",
url_segment="car-owners",
)
1.0.0rc3
1.0.0rc2
Included in this Release:
1.0rc #45 - @chrisbrantley
Includes the following fixes & modifications:
- Import relationship serializers by string
- Allow relationship URL segment override
- Fix relationship mixin view methods looking for non-existent self.relationship