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 django_modelapiview.responses import APIResponse
from .models import MyModel
class MyView(APIView):
# Required
route:str = "myroute" # The url to access your view
# Optional
enforce_authentification:bool = True # Should this model be restricted with Token access
def get(self, request, *args, **kwargs) -> APIResponse:... # One of head, options, get...
# views.py
from django_modelapiview import ModelAPIView
from .models import MyModel
class MyModelView(ModelAPIView):
# 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
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
]
def get(self, request, *args, **kwargs) -> APIResponse:... # One of head, options, get...
# 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
]
# You can use query parameters like order_by or limit (or customs):
https://myhost.com/api/mymodel/?order_by=-id&limit=1 # Will inverse order by id and limit to one : get the last id
# Or you can use Django defined filters:
https://myhost.com/api/mymodel/?id__in=1,2,3&foreignkey__id__in=2,3&field__lte=5
# And finally both:
https://myhost.com/api/mymodel/?manytomany__in=2,3&field__lte=5&limit=10
It also supports translation:
First enable it as an app
#settings.py
INSTALLED_APPS = [
...
'django_modelapiview',
...
]
Then change the language using GET parameters:
#Currently supports : enlish(en)(default), français(fr)
https://myhost.com/api/mymodel/?lang=fr
Using base views
Django ModelAPIView provides 2 base views:
- LoginView: to handle authentification (using the default Django authentification system)
- URLsView: to list the urls availables
Usage
# urls.py
from django.urls import path
from django_modelapiview.views import LoginView, URLsView # Importing them is enough
Errors
If you get a "Verb not implemented" reason from your endpoint but you are sure to have defined it.
You probable just forgot the -> APIResponse return type hint.
Metadata
Release files for django-modelapiview 1.4.15
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django_modelapiview-1.4.15.tar.gz | 13.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_modelapiview-1.4.15-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.9 kB
Release files / django_modelapiview-1.4.15.tar.gz
| Download URL | django_modelapiview-1.4.15.tar.gz |
|---|---|
| Size | 13.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fea33bac8a8a238522f7220b3fd61523430cfd492064c7cc59c6e0b39d08c40e
|
|
BLAKE2b-256 checksum How to use checksums |
93393c818fc7048a65b3f56665d11e739fc04c270b0bd81fe57b256b2288783c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6
|
Release files / django_modelapiview-1.4.15-py3-none-any.whl
| Download URL | django_modelapiview-1.4.15-py3-none-any.whl |
|---|---|
| Size | 14.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c5343f83a8612ff96cbf96eacea27d16a4ab984ec4261149a4f184d30c5a5732
|
|
BLAKE2b-256 checksum How to use checksums |
99152114e68116ec28d51a4dc20ce5b3eef845be3f77fb54f53be479641e4a96
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6
|
Security for django-modelapiview 1.4.15
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.