Professional Documents
Culture Documents
Orm in Django: Hoang Nguyen
Orm in Django: Hoang Nguyen
Hoang Nguyen
[Agenda]
ORM, What is?
Relational databases
Can store vast amounts of data in a structured way that allows for efficient
storage, access, and search
NoSQL solutions
truly vast datasets, real time and concurrent
[so, What problem]
key – value
Method Inher- Associ- Relation pair
itance ation
string sql = "select * from table where fname = '" + firstName + "' and lname
= '" + lastName + "'";
bunch of code
provides a set of APIs which allows access databases in OO style
helps database become more transparent
[ORM, What is]
1994: TopLink for SmallTalk – the first ORM
2001 – 2002: Hibernate
Entity Framework and LINQ
Gavin King
auto_now: automatically set the field to now every time the object is saved. False
by default. Note that the current date is always used; it’s not just a default value
that you can override.
auto_now_add: automatically set the field to now when the object is first created.
Note that the current date is always used; it’s not just a default value that you can
override.
[Some special fields]
A CharField that checks that the value is a valid email address. max_length=254
Inherits all attributes and methods from FileField, but also validates that the uploaded
object is a valid image.
height_field=None, width_field=None
Read API reference for the other field types
[How to create a primary key]
Must be unique for each record
Typically has a name of id
Typically is a auto-generate number
models.CharField(primary_key=True)
models.IntField(primary_key=True)
models.AutoField(primary_key=True)
[Exercise 1]
first_name: is a string, length <= 30, not null, not blank
mid_name: is a string, length <= 50, nullable, blankable
last_name: is a string, length <=30, not null, not blank
birthday: for storing the date of birth
admission_day: for storing the date and time
gender: F or M
SQLite
MySQL
Oracle
PostgreSQL
[How to sync between models and database]
Creates a package that contains all of the changes to be made on the database based
on the code changes in the models
--name
Synchronizes the database state with the current set of models and migrations
--fake
lazy
QuerySet has two public properties: ordered and db
Two main kinds of functions
return an instance of QuerySet class: all, filter, exclude, annotate, orderby, distinct, …
get, count, first, last, Avg, Count, Max, Min, Sum, Variance, StdDev, …
Q() objects
&, |, ~
Q(first_name__exact=‘Nguyen’) & Q(last_name__exact=‘Hoang’)