Django seldom uses SQL. It uses python class to access DB.
Pros. You can apply one code to various types of DB.
In django database,
- Data type is equal to table.
- Class member is like a table field.
Designing data model
Create data type(table) in ur sampleapp/models.py
from django.db import models
fieldA = models.IntegerField(unique=True)
Like IntegerField, there are also pre-defined field.
- TextField, DateTimefield, EmailField, URLField, FileField
Creating data type
Check if you activated your application in settings.py.
INSTALLED_APPS = (
Do DB sync
> ./manage.py syncdb
You can see which SQL were generated with,
> ./manage.py sql sampleapp
Note that it generate 'id' field automatically when it makes table.
Using DB Shell
Like we use database console to execute SQLs in terminal,
django also has shell.
> ./manage.py shell
>>> from sampleapp.models import *
>>> record = SampleTable(fieldA=123)
>>> records = SampleTable.objects.all()
>>> firstRecord = SampleTable.objects.get(id=1)
Like above, you can create a record, save it into DB, get all records or single one, access by id and delete it.
Note that until you save(), it just remain only in memory.
User data model
'User' data model is already built in.
>>> from django.contrib.auth.models import User
User has many attributes like username, email and password.