bout the MVC pattern for architecting web apps and focus on how to integrate databases in Django projects.
- MVC
- how does this relate to what we've done so far
- The "M" in MVC
- Adding a database to your Django project
- Building models with Django's ORM
- Django shell
- Django's built-in admin interface
Run python manage.py shell
to open an IPython shell with access to your app's database
- What is a database?
- Why do we need databases?
- Tables: picture an Excel spreadsheet
- Columns: attributes, name, data type
- Row: Record, represents a set of data for each column
- Primary key:
id
/pk
- Unique
- Can be an integer or UUID (349tu-hbe0984-5hww9e8y5h)
- Foreign key:
Allows you to associate to another table. i.e.
objectname_id
- ACID
- How to run
psql
- On Linux, simply run
psql
anywhere in the terminal - On OS X, first make sure postgres is running (you should see the elephant in your toolbar). If it isn't open spotlight (
cmd
+space
) and search for the Postgres application. Ifpsql
still doesn't work even though you see the elephant, try clicking on the elephant and telling it to start the Postgres server.
- On Linux, simply run
- Postgres commands:
\q
- quits from Postgres\l
- lists all the databases in the system
The complete Django db query interface can be found in the official documentation, but here are some highlights:
MyModel.create(name='someone', email='someone@somewhere.ca')
MyModel.objects.get(pk=1)
MyModel.objects.get(name='something')
MyModel.objects.all()
Exact match:
MyModel.objects.filter(category='animals')
Greater than:
MyModel.objects.filter(price__gt=10)
Less than or equal to:
MyModel.objects.filter(price__lte=100)
Notice the double underscores: __
(called 'dunder' in Python lingo).
MyModel.objects.filter(category='animals').count()
In ascending order:
MyModel.objects.order_by('price')
Or descending order:
MyModel.objects.order_by('-price')
The QuerySet returned can be treated like a list:
MyModel.objects.filter(category='animals')[0]
MyModel.objects.filter(category='animals')[100:200]
These functions have a separate page in the docs
You can import extra aggregation functions:
from django.db.models import Avg, Sum, Max, Min
And use them like so:
MyModel.objects.aggregate(Avg('price'))
example = MyModel.objects.get(pk=1)
example.delete()
example = MyModel.objects.get(pk=1)
example.name = 'Someone else'
example.save()
Chain .query
at the end of any Django query method to see what SQL is being composed and executed for you.