django-rest-crud is an example REST API project to help understand Django Rest Framework.
You can simply run the project by creating a virtual environment, install the required packages and making the migrations.
$ virtualenv -p python3 venv
$ source venv/bin/activate
$ cd django_rest_crud/
$ pip install -r requirements.txt
$ python manage.py makemigrations
$ python manage.py migrate
$ python manage.py runserver 0.0.0.0:80
- The project has two apps:
events
andsessions
. An event in the context of the project might be a gathering, conference, symposium etc. A session is a section of an event. - Events might have zero or more sessions. They can be created without specifying a session.
- Sessions cannot exist and be created without an event. They have one-to-many relationship with an event.
TIMEZONES = tuple(zip(pytz.all_timezones, pytz.all_timezones))
class Event(models.Model):
name = models.CharField(max_length=100, null=False, blank=False)
start_date = models.DateTimeField(null=False, blank=False)
end_date = models.DateTimeField(null=False, blank=False)
time_zone = models.CharField(max_length=32, null=False, blank=False, choices=TIMEZONES)
class Session(models.Model):
name = models.CharField(max_length=100, null=False, blank=False)
start_date = models.DateTimeField(null=False, blank=False)
end_date = models.DateTimeField(null=False, blank=False)
speaker = models.CharField(max_length=100, null=False, blank=False)
event = models.ForeignKey(Event, on_delete=models.CASCADE)
Retrieves all events in the database.
Method: GET
Url: /events/
Response Code: 200
Body:
[
{
"id": 2,
"name": "Example Event",
"start_date": "2020-12-15T18:00:26+03:00",
"end_date": "2020-12-15T18:00:26+03:00",
"time_zone": "UTC"
}
]
Creates a new event in the db
Method: POST
Url: /events/
{
"name": "Example Event",
"start_date": "2020-12-15 18:00:26",
"end_date": "2020-12-15 18:00:26",
"time_zone": "UTC"
}
Retrieves the created reasource along with its database id.
Response Code: 201
Body:
{
"id": 2,
"name": "Example Event",
"start_date": "2020-12-15 18:00:26",
"end_date": "2020-12-15 18:00:26",
"time_zone": "UTC"
}
Retrieves a single event
Method: GET
Url: /events/<event_id>/
Response Code: 200
Body:
{
"id": 2,
"name": "Example Event",
"start_date": "2020-12-15 18:00:26",
"end_date": "2020-12-15 18:00:26",
"time_zone": "UTC"
}
Updates a single event
Method: PUT
Url: /events/<event_id>/
Body:
{
"name": "Example Event",
"start_date": "2020-12-15 18:00:26",
"end_date": "2020-12-15 18:00:26",
"time_zone": "Europe/London"
}
Response Code: 200
Body:
{
"id": 2,
"name": "Example Event",
"start_date": "2020-12-15T18:00:26+03:00",
"end_date": "2020-12-15T18:00:26+03:00",
"time_zone": "Europe/London"
}
Deletes a single event
Method: DELETE
Url: /events/<event_id>/
Response Code: 204
Retrieves all sessions in the database including their event ids.
Method: GET
Url: /sessions/
Response Code: 200
Body:
[
{
"id": 2,
"name": "Example Session",
"start_date": "2020-12-15T18:00:26+03:00",
"end_date": "2020-12-15T18:00:26+03:00",
"speaker": "Berat",
"event": 3
}
]
Creates a new session in the db
Method: POST
Url: /sessions/
{
"name": "Example Session",
"start_date": "2020-12-15 18:00:26",
"end_date": "2020-12-15 18:00:26",
"time_zone": "UTC",
"speaker": "Berat",
"event": <event_id>
}
Retrieves the created reasource along with its database id.
Response Code: 201
{
"id": 3,
"name": "Example Session",
"start_date": "2020-12-15T18:00:26+03:00",
"end_date": "2020-12-15T18:00:26+03:00",
"speaker": "Berat",
"event": 3
}
Retrieves a single session
Method: GET
Url: /sessions/<session_id>/
Response Code: 200
Body:
{
"id": 3,
"name": "Example Session",
"start_date": "2020-12-15T18:00:26+03:00",
"end_date": "2020-12-15T18:00:26+03:00",
"speaker": "Berat",
"event": 3
}
Updates a single session
Method: PUT
Url: /sessions/<session_id>/
Body:
{
"name": "Different Session",
"start_date": "2020-12-15T18:00:26+03:00",
"end_date": "2020-12-15T18:00:26+03:00",
"time_zone": "UTC",
"speaker": "random_speaker",
"event": 3
}
Response Code: 200
Body:
{
"id": 3,
"name": "Different Session",
"start_date": "2020-12-15T18:00:26+03:00",
"end_date": "2020-12-15T18:00:26+03:00",
"speaker": "random_speaker",
"event": 3
}
Deletes a single session
Method: DELETE
Url: /sessions/<session_id>/
Response Code: 204