Notes Re Django Documentation
Models
- a model is like a column/row/table of an excel file, it contains essential fields and behaviours of the data stored
- they all inherite
django.db.models.Model
class - after setting up the model classes, you can access the database through the API generated automatically by Django: see Making queries
- Adding apps to settings
INSTALLED_APPS
: this is to tell Django that there’s new models you’d like to use and you’ve set them up, after that, you need tomakemigrations
andmigrate
for the models to take effect, meaning tables are created in the database for you to use later - Model fields: determine the column type, the default HTML widget to use to render a form field, validation; model field reference lists all the fields a model class can implement
- Field common used options:
- null=True, database related, doesn’t have to exist at all
- blank=True(can be not filled), validation on forms won’t be required
- choices=((‘s’, ‘small’), (‘m’, ‘medium’), (‘l’,’large’)) a sequence of 2-tuples,
- default: a value or a callable object, everytime the new object is created, it will be called
- unique=True, this field must be unique throughout the table common model field option reference
- Model Meta options
- Model methods: define functions/methods on a model is adding custom “row-level” functionality to the object.
- Model inheritance:
Making Queries
- filters: Retrieving specific objects with filters
Entry.objects.all()[:5]
- first 5 entry objects (LIMIT 5)Entry.objects.all()[5:10]
- (OFFSET 5 LIMIT 5)Entry.objects.all()[:10:2]
- return a list of every second object of the first 10- chaning filters:
The result of refining a QuerySet is itself a QuerySet, so it’s possible to chain refinements together. For example:
>>> Entry.objects.filter( ... headline__startswith='What' ... ).exclude( ... pub_date__gte=datetime.date.today() ... ).filter( ... pub_date__gte=datetime.date(2005, 1, 30) ... )
This takes the initial QuerySet of all entries in the database, adds a filter, then an exclusion, then another filter. The final result is a QuerySet containing all entries with a headline that starts with “What”, that were published between January 30, 2005, and the current day.
Django Template Language
- built-in filters:
{ bio|truncatewords:30}
display the first 30 words of the bio variable{ list|join:", "}
join a list with commas and spaces{ value|filesizeformat }
human-readable file size: If value is 123456789, the output would be 117.7 MB.{ value|length }
returns the length of a string or a list{ value|first }
and{ value|last }
return the first or the last of a string/list
- Comments:
{# comment out part #}
- you can access the methods in a class by dot notation in template as well
URL Dispatcher
\<slug:slug\>
at the end of url dispatcher: matches any slug string consiting of ASCII letters or numbers, plus the hyphen and underscore characters.from django.urls import re_path
for regex:re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),)
- you can pass extra options to view function: here
Request and Response Objects
To be read