This file handles the CRUD operations.
Swords
, the resource
name is Sword
and the file name is SwordResource
. The endpoint for CRUD operations are:
Action | HTTP method | Endpoint |
---|---|---|
Create | POST | /swords |
List | GET | /swords |
Detail | GET | /swords/:id |
Update | PUT | /swords/:id |
Delete | DELETE | /swords/:id |
teams
resource:
Action | HTTP method | Endpoint |
---|---|---|
Create | POST | /teams/:id/swords |
List | GET | /teams/:id/swords |
Detail | GET | /swords/:id |
Update | PUT | /swords/:id |
Delete | DELETE | /swords/:id |
GET /swords
.
Create a file called SwordResource
that is a subclass of 1 of the following:
GenericResource
: subclass this class if you want to execute arbitrary codeDatabaseResource
: subclass this class only if your resource is directly associated to a
database model. This subclass has many methods defined by default that handles CRUD operations
automatically.GenericResource
backend/api/resources/SwordResource.py
:
DatabaseResource
POST /swords
.
GenericResource
DatabaseResource
create
class method. The instances where it is overrided
include a scenario when you want to execute a callback function only if the resource was
successfully created.
The DatabaseResource
create
method will take the keys and values in the payload argument, assign
those values to a newly instantiated Sword
object, and then save the model to the database.
GET /swords/:id
.
GenericResource
DatabaseResource
member
method.
If you do, see TrainingRunResource
for an example.
PUT /swords/:id
.
This is almost the same as the create
method except it’s an instance method and the ID of the
resource you want to update must be included in the URL.
DatabaseResource
update
method.
If you do, see ModelVersionResource
for an example.
DELETE /swords/:id
.
DatabaseResource
delete
method.
POST /teams/:id/swords
GET /teams/:id/swords
sword.user_id
column. If you are fetching a list of 100
swords and in your code you are accessing the methods of the user
object via sword.user
(e.g. sword.user.name
), you will inadvertently make 100 extra API calls to the users
database table. Do the following to prevent this:
swords.opponents.objects.all()
method. This will fetch all opponents from the database where
opponent.sword_id
equals sword.id
. If you are accessing the opponents of each sword when
fetching a list of 100 swords, you will be making 100 extra API calls to the opponents
database.
Do the following to prevent this: