Skip to main content

Nexios-Generic is an extension for the Nexios framework that provides a class-based view implementation, integrating seamlessly with Tortoise ORM and Pydantic for efficient, structured API development. It simplifies request handling, validation, and database interactions, enabling a more declarative and reusable approach to building Nexios applications.

Project description

This documentation provides a comprehensive guide to using nexios-generic built on top of Nexios, a lightweight Python web framework. The framework is designed to simplify the creation of CRUD (Create, Read, Update, Delete) APIs using Tortoise ORM for database management and Pydantic for data validation. While this framework is a side project and not extensively tested, it provides a solid foundation for building APIs quickly and efficiently.


Table of Contents

  1. Core Components
  2. Getting Started
  3. Usage Guide
  4. Advanced Usage
  5. Example Project
  6. Conclusion

Core Components

GenericAPIView

The GenericAPIView class is the base class for all views in the framework. It provides the foundational logic for handling requests, validating data, and formatting responses. Key methods include:

  • get_queryset(): Returns the queryset for the view.
  • get_pydantic_class(): Returns the Pydantic model class used for data validation and serialization.
  • validate_request(): Validates the incoming request data using the Pydantic model.
  • get_object(): Retrieves a single object from the database based on the lookup field.
  • format_error_response(): Formats an error response with a status code and details.
  • format_success_response(): Formats a success response with data and a status code.

Mixins

Mixins provide reusable functionality for common CRUD operations. They are designed to be combined with GenericAPIView to create views with specific behavior.

  • CreateModelMixin: Handles creating new objects.
  • RetrieveModelMixin: Handles retrieving a single object.
  • ListModelMixin: Handles listing multiple objects.
  • UpdateModelMixin: Handles updating existing objects.
  • DeleteModelMixin: Handles deleting objects.

Views

The framework provides pre-built views that combine the mixins for common use cases:

  • ListCreateAPIView: Combines ListModelMixin and CreateModelMixin for listing and creating objects.
  • RetrieveUpdateDestroyAPIView: Combines RetrieveModelMixin, UpdateModelMixin, and DeleteModelMixin for retrieving, updating, and deleting objects.
  • ListCreateRetrieveUpdateDestroyAPIView: Combines all mixins for full CRUD functionality.

Getting Started

Installation

Before using the framework, ensure you have the following installed:

  • Python 3.7 or higher
  • Nexios (a lightweight Python web framework)
  • Tortoise ORM (for database management)
  • Pydantic (for data validation)

You can install the required packages using pip:

pip install nexios tortoise-orm pydantic

Project Setup

  1. Initialize Nexios: Create a new Nexios application.
  2. Configure Tortoise ORM: Set up your database configuration.
  3. Define Models: Create your database models using Tortoise ORM.
  4. Define Pydantic Schemas: Create Pydantic schemas for data validation and serialization.
  5. Create Views: Use the provided mixins and views to create your API endpoints.
  6. Add Routes: Add your views as routes in your Nexios application.

Usage Guide

Defining Models

Define your database models using Tortoise ORM. For example, a User model:

from tortoise import fields, models

class User(models.Model):
    id = fields.IntField(pk=True)
    username = fields.CharField(max_length=50, unique=True)
    email = fields.CharField(max_length=100, unique=True)

    class Meta:
        table = "users"

Defining Pydantic Schemas

Define Pydantic schemas for data validation and serialization. For example, a UserSchema:

from pydantic import BaseModel

class UserSchema(BaseModel):
    id: int | None = None
    username: str
    email: str

    class Config:
        from_attributes = True

Creating Views

Use the provided mixins and views to create your API endpoints. For example, to create a view for listing and creating users:

from src.views import ListCreateAPIView
from src.base import GenericAPIView

class UserView(ListCreateAPIView):
    pydantic_class = UserSchema

    async def get_queryset(self):
        return User.all()

Adding Routes

Add your views as routes in your Nexios application:

from nexios import get_application

app = get_application()
app.add_route(UserView.as_route("/users"))

Advanced Usage

Customizing Querysets

You can customize the queryset used by your views by overriding the get_queryset() method. For example, to filter users by a specific condition:

class UserView(ListCreateAPIView):
    pydantic_class = UserSchema

    async def get_queryset(self):
        return User.filter(username__icontains="admin")

Customizing Validation

You can customize the validation logic by overriding the validate_request() method. For example, to add custom validation rules:

class UserView(ListCreateAPIView):
    pydantic_class = UserSchema

    async def validate_request(self):
        data = await self.request.json()
        if "username" not in data:
            raise ValueError("Username is required")
        return self.pydantic_class(**data)

Error Handling

You can customize error handling by overriding the error-handling methods in the mixins. For example, to customize the error response format:

class UserView(ListCreateAPIView):
    pydantic_class = UserSchema

    def handle_validation_error(self, res, error):
        return res.status(400).json({"error": "Validation failed", "details": str(error)})

Extending Mixins

You can extend the mixins to add custom behavior. For example, to add logging to the CreateModelMixin:

class LoggingCreateMixin(CreateModelMixin):
    async def perform_create(self, instance):
        print(f"Creating object: {instance}")
        return await super().perform_create(instance)

Example Project

Here’s a complete example of a simple API for managing users:

from tortoise import fields, models
from pydantic import BaseModel
from src.views import ListCreateAPIView
from nexios import get_application

# Define the User model
class User(models.Model):
    id = fields.IntField(pk=True)
    username = fields.CharField(max_length=50, unique=True)
    email = fields.CharField(max_length=100, unique=True)

    class Meta:
        table = "users"

# Define the Pydantic schema
class UserSchema(BaseModel):
    id: int | None = None
    username: str
    email: str

    class Config:
        from_attributes = True

# Create the view
class UserView(ListCreateAPIView):
    pydantic_class = UserSchema

    async def get_queryset(self):
        return User.all()

# Add the route to Nexios
app = get_application()
app.add_route(UserView.as_route("/users"))

Conclusion

This framework provides a lightweight and easy-to-use solution for building CRUD APIs with Nexios, Tortoise ORM, and Pydantic. While it is not extensively tested, it offers a solid foundation for quickly creating APIs. Feel free to extend and modify the framework to suit your needs!

For more information on Nexios, visit the official documentation: Nexios Documentation.


GitHub Badges

GitHub License GitHub Issues GitHub Stars GitHub Forks


Logo

Nexios Logo


Thank you for using this framework! If you have any questions or feedback, please open an issue on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nexios_generic-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nexios_generic-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file nexios_generic-0.1.0.tar.gz.

File metadata

  • Download URL: nexios_generic-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Windows/10

File hashes

Hashes for nexios_generic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7ed466db3a3b139b4ff27275dd3c423c771b5518e46b28915c5a1add3220e88a
MD5 af6e1a62b854da3fff1c6678e3b88c44
BLAKE2b-256 8e4fca73b5d1aded18e7fd43a2d2878fa2cd80e84487c5ebee373bb5338977d6

See more details on using hashes here.

File details

Details for the file nexios_generic-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nexios_generic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Windows/10

File hashes

Hashes for nexios_generic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c89168db1e388327fa8a5c2953ec672927504fe790a98f8527abbb6154de15e
MD5 d71b504fc03a4423ef3ff75e0f0d6e54
BLAKE2b-256 1580faa613dfc4c5ab15d2cdd889ca89ec3da3efab38d9550fc676f0fdd947f8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page