What's Changed
News:
* Many to many relation serialization support by caspel26 in https://github.com/caspel26/django-ninja-aio-crud/pull/7
Bugs:
* Bugfix: router path name in model CRUD router by caspel26 in https://github.com/caspel26/django-ninja-aio-crud/pull/7
* Bugfix: OneToOne reverse and forward relations by caspel26 in https://github.com/caspel26/django-ninja-aio-crud/pull/7
Improvements:
* Improved models serializeration by caspel26 in https://github.com/caspel26/django-ninja-aio-crud/pull/7
🎉 ManyToMany schemas serialization support
* many to many support is finally here. you can define your models and insert the many to many field in reverse and forward relations in ReadSerializerClass.
Python
models.py
from django.db import models
from .ninja_aio.models import ModelSerializer
class Bar(ModelSerializer):
name = models.CharField(max_length=30)
description = models.TextField(max_length=30)
class ReadSerializer:
fields = ["id", "name", "description", "foos"]
class CreateSerializer:
fields = ["name", "description"]
class UpdateSerializer:
fields = ["name", "description"]
class Foo(ModelSerializer):
name = models.CharField(max_length=30)
active = models.BooleanField(default=False)
bars = models.ManyToManyField(Bar, related_name="foos")
class ReadSerializer:
fields = ["id", "name", "active", "bars"]
class CreateSerializer:
fields = ["name", "active"]
class UpdateSerializer:
fields = ["name", "active"]
* Then add APIViewSets.
Python
views.py
from ninja import NinjaAPI
from .ninja_aio.views import APIViewSet
from .ninja_aio.parsers import ORJSONParser
from .ninja_aio.renders import ORJSONRenderer
from . import models
api = NinjaAPI(parser=ORJSONParser(), renderer=ORJSONRenderer())
class FooAPI(APIViewSet):
model = models.Foo
api = api
class BarAPI(APIViewSet):
model = models.Bar
api = api
FooAPI().add_views_to_route()
BarAPI().add_views_to_route()
* And that's it! Django Ninja Aio Crud will create dinamically all the schemas that you need and resolve all the relations! If you want to add a view to add, for example, a "bar" or multiple "bars" instances to Foo it could be something like that.
Python
views.py
from ninja import Schema
from ninja_aio.schemas import GenericMessageSchema
class AddBarsSchema(Schema):
bars: list[int]
class FooAPI(APIViewSet):
model = models.Foo
api = api
def views(self):
self.router.patch(
"{id}/add-bars/", response={200: self.schema_out, 404: GenericMessageSchema}
)
async def add_bars(request: HttpRequest, id: int, data: AddBarsSchema):
try:
foo = await models.Foo.objects.prefetch_related("bars").aget(pk=id)
except models.Foo.DoesNotExist:
return 404, {"foo": "not found"}
for bar_id in data.bars:
try:
bar_obj = await models.Bar.objects.aget(pk=bar_id)
except models.Bar.DoesNotExist:
return404, {"bar": "not found"}
await foo.bars.aadd(bar_obj)
await foo.asave()
foo = await models.Foo.objects.prefetch_related("bars").aget(pk=id)
return 200, foo
**Full Changelog**: https://github.com/caspel26/django-ninja-aio-crud/compare/v0.1.4...v0.2.0