Getting started
Anvil installs into an existing Django + REST Framework project โ it isn't a replacement for either. If you don't have a project yet, make one first:
# new project, if you need one
python3 -m venv .venv && source .venv/bin/activate
pip install django djangorestframework django-filter
django-admin startproject config .
python manage.py startapp products
Install into a project
pip install django-anvil
Register it in settings.py:
INSTALLED_APPS = [
...,
"rest_framework",
"django_filters",
"django_anvil",
"products",
]
RBAC and multi-tenancy are opt-in apps of their own
(django_anvil.rbac, django_anvil.tenancy) โ only add them once you
actually use permissions or tenant_scoped. See their own pages.
Define a model and a Resource
# products/models.py
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.IntegerField(default=0)
# products/resources.py
from django_anvil.core.resource import Resource
from .models import Product
class ProductResource(Resource):
model = Product
fields = ["id", "name", "price", "stock"]
read_only_fields = ["id"]
searchable = ["name"]
filters = []
sortable = ["price"]
Generate everything
python manage.py migrate
python manage.py anvil resource Product
That writes/merges into products/api/serializers.py, products/api/views.py,
products/api/urls.py, registers ProductAdmin, and writes
products/tests/test_product_api.py. If the app has more than one Resource, a second
anvil resource Coupon adds its classes alongside Product's in the same
files โ the way a person would by hand, not by overwriting them.
Wire the URLs
One-time, the first time you generate a Resource in a given app:
# config/urls.py
path("api/", include("products.api.urls")),
Run it: python manage.py runserver, then /api/products/ is a full CRUD API
with search, filtering, and ordering already wired from searchable/filters/sortable.
Regenerating with --force
By default, re-running anvil resource never touches a file that already has
content โ safe to run repeatedly, and safe against clobbering hand-edits. Once you change a
Resource's declaration (add a field, turn on tenant_scoped, add permissions),
regenerate that Resource's block specifically:
python manage.py anvil resource Product --force
--force only replaces that Resource's block in each shared file โ it never
touches another Resource's code in the same file, and it never touches admin.py's
registration (which routinely holds hand-written admin customizations) โ see
multiple Resources in one app.
See everything currently registered, or check the project for common mistakes, any time:
python manage.py anvil list ยท python manage.py anvil doctor