How to create an Installable Django App

Overview

Example migration

Here is the (beefy) view in Django REST Framework:

class CommandPaletteView(BaseAPIView):
    """
    An API endpoint to return custom resources to the command palette on the frontend.
    """

    def get(self, request: Request) -> Response:
        logger.info(
            f"[CommandPaletteView.get] received request with data {request.data}"
        )
        buildings = Building.objects.filter(
            property_owner__property_management_company=request.user.property_management_company
        )

        return Response(
            [
                {"name": building.street_address, "url": building.frontend_detail_url}
                for building in buildings
            ]
        )

...and here is the (cleaner) view in Django-Ninja:

@api.get(
    "/command-palette/",
    response=List[CommandPaletteSchema],
    auth=JWTAuth(),
)
def get_command_palette_data(
    request: AuthenticatedWSGIRequest
) -> List[dict[str, str]]:
    return [
            {"name": building.street_address, "url": building.frontend_detail_url}
            for building in Building.objects.filter(
            property_owner__property_management_company=request.user.property_management_company
        )
        ]

Final Thoughts

If you enjoyed this article, be sure to join my Developer Monthly newsletter, where I send out the latest news from the world of Python and JavaScript:


Written on January 22nd, 2021