Skip to content

REST API

Håvard Hellem edited this page Mar 7, 2018 · 27 revisions

Overview

API root is located at /api/

Will return the API Root with links to the other endpoints of the api.

How to use

As of right now everything is in development

Planned features

Endpoints

Sprint 1

TODO: Specify the endpoints

  • post road network:
  • post production data:

Later

  • Retrieve road status for a given geographic area. How long since last time the road stretch was maintained and how much snow since last maintenance etc. (To be used for showing status on map.)
  • Retrieve a stretch of road.

How to

Put api root at /api/

In backend/urls.py include your api app's urls.py file.

urlpatterns = [
    url(r'^', include('api.urls')),
    ...
]

This should be near the top of urlpatterns, because it is prioritized by order.

In your api/urls.py file, the urlpatterns router include should be set to api/

urlpatterns = [
    url(r'api/', include(router.urls))
]

Short explanations (and possibly some tip and tricks)

See the Django and Django Rest Framework documentation for more informantion

Views

Serializers

A serializer allow complex data such as model instances to be converted to native Python data types that can be rendered into html or json. It also provide deserialization, allowing data to be converted back to complex data.

The Meta class

The Meta class has many different options that can be set.

Model

The model option specifies which model this serializer uses (database model).

Fields

The fields option selects which fields from the model the serializer uses when serializing/deserializing.

Models

Meta class

Similar to serializers the models supports metadata.

Abstract

The abstract option sets the model to be abstract. This can be used to make a template model with base fields if you want several of your models to have some of the same fields.

Example:

class BaseModel(models.Model):
    id = models.AutoField(primary_key=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

Usage:

class ProductionData(BaseModel):

This will include the three fields of BaseModel in ProductionData. The id field is the primary key and will auto increment. It will (should) also make it so the created field is set to the date at which the row was created and the updated field should be set to the current datetime every time the row is updated. Fields with auto_now_add=True and auto_now=True also have editable=False by default

Clone this wiki locally