Django API
Once you’ve created your data models, Django automatically gives you a database-abstraction API that lets you create, retrieve, update and delete objects. This document explains how to use this API. Refer to the data model reference for full details of all the various model lookup options.
https://docs.djangoproject.com/en/2.0/topics/db/queries/
So our testmodel is a simple Note where we store text from the user. This is our base for e-lo on web.
We feed e-lo some text and select a of several NLP functions that will be implemented later (Some of them are implemented, but must interfaced with Django, now they are interfaced with class cmd.Cmd([completekey[, stdin[, stdout]]]).
We have also added the __str__ method for a better output then just the object representation with id.
Our model class in now in the C:\Users\espen\virtualEnvs\e-loweb\eloweb\elo
C:\Users\espen\virtualEnvs\e-loweb\eloweb holds all the Django configuration and \elo\ holdes the app configuration from cmd:
After installing Django, you create your app:
(env) Folder>python manage.py startapp elo
Django database API:
Creating objects:
This performs an INSERT SQL statement behind the scenes. Django doesn’t hit the database until you explicitly call save().
Update objects:
To save changes to an object that’s already in the database, use save().
This performs an UPDATE SQL statement behind the scenes. Django doesn’t hit the database until you explicitly call save().
If you’re just updating a record and don’t need to do anything with the model object, the most efficient approach is to call update(), rather than loading the model object into memory.
Retrieving objects:
Most of the time you’ll use all(), get(), filter() and exclude() when you need to look up objects from the database. However, that’s far from all there is; see the QuerySet API Reference for a complete list of all the various QuerySet methods.
https://docs.djangoproject.com/en/2.0/ref/models/querysets/#queryset-api
Deleting objects:
The delete method, conveniently, is named delete(). This method immediately deletes the object and returns the number of objects deleted and a dictionary with the number of deletions per object type.
Now lets add a new method to our class and call it.
And call to_string()
Now lets change the model with a new field and run:
(env) file>python manage.py makemigrations
(env) folder>python manage.py migrate
Model field reference:
https://docs.djangoproject.com/en/2.1/ref/models/fields/#field-types
Now lets access the field note_published:
You can also access the Django Admin.
(env) Folder->python manage.py runserver
And visit the web url:
Add a new note:
And that complets the Django API CRUD and models migration.