Django Basics #

Authentication Backends #

Authentication backends in Django are simply classes that implement get_user and authenticate methods given a primary key and credentials respectively. If multiple authentication backends are specified in the configuration, Django will try all available backends until a successful authentication is found

Custom User Models #

It is recommended to create a custom user model whenever you are starting a new Django project. To maintain all the built in functionality of password hashing, etc you can inherit from BaseUser and point AUTH_USER_MODEL to the custom user

Automatic Primary Keys #

By default Django gives every table the field id = models.AutoField(primary_key=True) which is an auto updating primary key

Random Tidbits #

Meta API #

Field API #

Models #

Meta classes in models represent anything that is not a field, e.g. ordering, column names, etc.

Model Managers #

Model Managers are the most important part of the model as they retrieve instances from the database and provide the database queries. The default model manager is called objects and can be referenced by <model_name>.objects if not overriden

Model Logic #

It is best practice to include necessary simple custom model functions in the models. These can go with other model functions such as __str__ and can be referenced by <model_Instance>.<method_name>;

Pre-defined model methods #

Django provides pre-defined model methods such as save() and delete() than can be overridden in the model class by simply redefining save(*args, **kwargs) and save(*args, **kwargs). Calling super(*args, **kwargs) in this methods will ensure the data still gets saved.

Theses pre-defined methods are why, given an example model Post

b = Post(text)
b.save()

saves a new Post to the database. save will update a model if it already exists.

I believe Post(text) provides a constructor that can also be accessed by Post.objects.create(text)

Querysets #

A query set is essentially a select SQL statement with as many filters as desired which act as the where statement. Querysets come from the model’s manager and thus NOT a model instance.

A queryset does not actually hit the database until it is iterated through or another form of evaluation. <Model></Model>.objects.all() will not evaluate the queryset and thus will not hit the database

You can easily chain queryset filters together. This chaining is immutable and returns a new queryset every time

Admin #

Django Rest Frameword #

Testing #

General Tips #