An APIView to handles its model endpoints
Project description
Django_APIView
Implement a base generic view for handling model RESTful endpoints
Usage
# models.py
from django.db import models
from django_modelapiview import JSONMixin
class MyModel(JSONMixin, models.Model):
"""
Declare your model as you usually do but
add a json_fields list
"""
json_fields:list[str] = ['text', 'image', 'fk', 'fk_reverse', 'm2m', 'my_method']
text = models.TextField()
image = models.ImageField()
fk = models.ForeignKey(...)
# fk_reverse
m2m = models.ManyToManyField(...)
def my_method(self):
return "my custom value"
# views.py
from django_modelapiview import APIView
from .models import MyModel
class MyView(APIView):
# Required
model:JSONMixin = MyModel # Your model
route:str = "mymodels" # The url to access your collection
# Optional
queryset:QuerySet = MyModel.objects.all() # A custom base queryset (will be affected by query filters)
singular_name:str = "my model" # Singular name of your model for reason message
plural_name:str = "my models" # Plural name of your model for reason message
http_method_names:list[str] = ['head', 'get', 'patch', 'post'] # The list of HTTP method names that this view will accept.
enforce_authentification:bool = True # Should this model be restricted with Token access
query_parameters:list[tuple[str, Callable[[QuerySet, object], QuerySet]]] = [
('order_by', lambda queryset, field_names: queryset.order_by(*field_names.split(",")) if field_names else queryset),
('limit', lambda queryset, limit: queryset[:int(limit)] if limit else queryset), # Should be last since sliced QuerySet can't be filtered anymore
]
# urls.py
from django.urls import path, include
from . import views
urlpatterns = [
path("", include("django_routeview")), # Django RouteView are used as based class for APIView in order to automatically register them
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django_modelapiview-1.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 388da0254e56b078fe2e311b6fb51e34d050c5f5561621958e416417e45b47a1 |
|
MD5 | 8db60ead758640b5fdcc8047279ab269 |
|
BLAKE2b-256 | 8ff1f58f6bbbba61278187f84257c70aa685414f45145a5809a95ef6186edf15 |
Close
Hashes for django_modelapiview-1.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1704c62a4ef55b2d774b0e0f7bea4b9a225a4f0d099ec80d1f52d7f2a524cb8 |
|
MD5 | 2b024fd59a729a7efa56433167e8f29b |
|
BLAKE2b-256 | 18485fa6e5984e6082bd1f9adfd4f6e3ba4b58e788db9af1d4e8086fc53708d6 |