First CRUD
We assume you have successfully set up your project following the Introduction guide.
In this guide, we will create a CRUD
(Create
, Retrieve
, Update
, and Delete
) API for managing Book
entities.
Project Structure#
The final structure of your project will be as follows:
.
├── app
│ ├── apis.py
│ ├── models.py
│ ├── serializers.py
│ └── urls.py
└── core
├── configs.py
└── urls.py
Configuring the Database#
How does the database work in Panther?
Refer to Database to learn about supported databases and their functionality.
Configure the DATABASE
settings in core/configs.py
. In this guide, we will use PantherDB
.
PantherDB is a simple, file-based, document-oriented database.
DATABASE = {
'engine': {
'class': 'panther.db.connections.PantherDBConnection',
}
}
Defining the Model#
How do models work in Panther?
Refer to Models to learn more about defining models and how they function.
File Handling in Models
For models that need to handle file uploads, see File Handling for detailed information about using File
and Image
types in your models.
Create a model named Book
in app/models.py
:
app/models.py | |
---|---|
1 2 3 4 5 6 7 |
|
Defining URLs#
How do URLs work in Panther?
Refer to URLs to learn more about URL definitions.
The base urls
configuration should include all application URLs.
core/urls.py | |
---|---|
1 2 3 4 5 6 |
|
app/urls.py
, define the Book
API URLs:
app/urls.py | |
---|---|
1 2 3 4 5 6 |
|
app/urls.py | |
---|---|
1 2 3 4 5 6 |
|
Defining the Serializer#
How do serializers work in Panther?
Refer to Serializer to learn more about available serializers.
File Handling in Serializers
For serializers that need to handle file uploads, see File Handling for information about validating and processing files in your serializers.
Serializers transform data between the application and API requests.
The serializer can be inherited from ModelSerializer
or pydantic.BaseModel
app/serializers.py | |
---|---|
1 2 3 4 5 6 7 8 |
|
app/serializers.py | |
---|---|
1 2 3 4 5 6 7 8 |
|
The serializer should be inherited from ModelSerializer
to be used in GenericAPI
.
app/serializers.py | |
---|---|
1 2 3 4 5 6 7 8 |
|
APIs#
How do APIs work in Panther?
Refer to API to learn more about API types and their usage.
File Upload APIs
For APIs that handle file uploads, see File Handling for comprehensive examples of file upload endpoints and processing.
Create#
app/apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
app/apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
app/apis.py | |
---|---|
1 2 3 4 5 6 7 |
|
List#
app/apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
app/apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
app/apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Pagination
class will look for thelimit
andskip
in thequery params
and paginate your response and then return it on its own response template.- The query will be changed and looking for the value of the
search
query param in these fields, e.g. query param is the?search=TheLittlePrince
, we will looking for the Book withname
orauthor
ofTheLittlePrince
. - It will look for each value of the
filter_fields
in thequery params
and query on them, e.g.?name=Something&author=Ali
, it will looks for Book that itsauthor
isAli
and itsname
isSomething
. - The query will be sortalbe with the fields which is in
sort_fields
.
Retrieve#
app/apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
app/apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
app/apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Update#
app/apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
app/apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
app/apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Delete#
apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 |
|
apis.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
With this, you now have a complete CRUD API implementation for the Book
entity.