-
Jean-Baptiste authoredJean-Baptiste authored
Synopsis
This module is an add-on for Django REST Framework that serves a django model respecting the Linked Data Platform convention.
It aims at enabling people with little development skills to serve their own data, to be used with a LDP application.
Requirements
- Django (known to work with django 1.11)
- Django Rest Framework
- pyld
- django-guardian
- djangorestframework-guardian
Installation
- Install this module and all its dependencies
pip install djangoldp
- Create a django project
django-admin startproject myldpserver
- Create your django model inside a file myldpserver/myldpserver/models.py Note that container_path will be use to resolve instance iri and container iri In the future it could also be used to auto configure django router (e.g. urls.py)
from djangoldp.models import Model
class Todo(Model):
name = models.CharField(max_length=255)
deadline = models.DateTimeField()
3.1. Configure container path (optional) By default it will be "todos/" with an S for model called Todo
<Model>._meta.container_path = "/my-path/"
3.2. Configure field visibility (optional) Note that at this stage you can limit access to certain fields of models using
<Model>._meta.serializer_fields (<>list of field names to show>)
For example, if you have a model with a related field with type django.contrib.auth.models.User you don't want to show personal details or password hashes.
E.g.
from django.contrib.auth.models import User
User._meta.serializer_fields = ('username','first_name','last_name')
Note that this will be overridden if you explicitly set the fields= parameter as an argument to LDPViewSet.urls(), and filtered if you set the excludes= parameter.
- Add a url in your urls.py:
from django.conf.urls import url
from django.contrib import admin
from djangoldp.views import LDPViewSet
from .models import Todo
urlpatterns = [
url(r'^', include('djangoldp.urls')),
url(r'^admin/', admin.site.urls), # Optional
]
This creates 2 routes for each Model, one for the list, and one with an ID listing the detail of an object.
You could also only use this line in settings.py instead:
ROOT_URLCONF = 'djangoldp.urls'
- In the settings.py file, add your application name at the beginning of the application list, and add the following lines
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
LDP_RDF_CONTEXT = 'https://cdn.happy-dev.fr/owl/hdcontext.jsonld'
- You can also register your model for the django administration site
from django.contrib import admin
from .models import Todo
admin.site.register(Todo)
-
You then need to have your WSGI server pointing on myldpserver/myldpserver/wsgi.py
-
You will probably need to create a super user