Menu
e-lo
  • Home
    • Tech
    • Inspiration and about
  • Database
    • T-SQL
    • SQL Server quick
    • SQL server docs
    • MySql quick sheet
    • Postgre
    • InfluxDB
  • Programming
    • Automating the boring stuff
    • Python 101
    • Python Docs
    • Python Logging
    • cSharp Overview
    • Powershell Latest
    • Powershell 4 lang ref
  • Linux
    • Top CMD’s
    • Useful CMD Linux
    • ss64 Linux
    • Ubuntu
    • 30 things Ubuntu 18.04
  • Azure
    • AZ-104-MS Azure Administrator 101 quick ref
    • ARM Docs
    • ARM Tutorial
    • Azure PS ref
    • Azure CLI ref
    • Deployment scripts (intern/extern)
    • ARM Quickstart
    • ARM templates 4h
  • AzAdm
    • AD 101
    • Governance and Compliance 102
    • Administration 103
    • Virtual Networking 104
    • Storage 107 (With table (NoSQL and more))
    • Virtual Machines 108
    • Azure Virtual Machines 101
    • Monitor VM (and market)
  • Zen
    • Not thinking about anything is Zen
e-lo

Django database API migrations and models 101

Posted on August 8, 2018November 4, 2018 by espenk

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

note_published = models.BooleanField(default=False)

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.

RSS Azure

  • Azure and HITRUST publish shared responsibility matrix January 14, 2021

RSS Python

  • PEP 649: Deferred Evaluation Of Annotations Using Descriptors January 11, 2021

Cloud

ARM (7) azure (22) cmd (1) Django (4) Docker (1) e-lo (2) Flask (2) Github (9) Grafana (2) Information (1) Information Retrieval (11) JAVA (1) kivy (2) Kotlin (4) linux (11) mobile (2) Natural Language Prossesing (NLP) (2) Net.Core (1) Networking and Security (2) OPC (2) PEP8 (1) Philosophy (3) Python (41) Python Networking and Security (2) Reason (2) RMQ (2) Solr (11) Sql (10) VSC (1) Warframe (2) WMVARE (4) Zabbix (7)

Recent Posts

  • 2 TODO MS ARM Template 4h
  • TODO Cryptography with Python – Caesar Cipher
  • 3 TODO Udemy AZ-104 Microsoft Azure Administrator Exam Certification (Scott Duffy)
  • 1 TODO ARM Lab 104 MS (Deployment and more)
  • TODO Prerequisites for Azure administrators 101

Archives

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2021 e-lo | Powered by WordPress & Superb Themes