There are three types of inheritance that Django supports
- Abstract Base Class
- Multi-table Inheritance
- Proxy Models
Abstract Base Class
- Parent Class has attributes common for many child classes
- Parent Class is only used for inheritance, not saved in database
- In Parent's Meta class, 'abstract' is marked as True
- Child class can inherit from many parent classes.
- Parent class can't be used for creating objects
class Person(models.Model):
# common fields
class Meta:
abstract = True
class Student(Person):
pass
class Teacher(Person):
pass
Person() #Not callable|Can't create objects
Multi-table inheritance
- Every model is a model in itself.
- One-to-One link is created b/w tables automatically.
class Student(models.Model):
# Student Attributes
pass
class Teacher(Student):
# Teacher inherits the properties of Student
pass
Proxy Models
- Proxy(something authorized to act on behalf of another) models altering some properties of base model like ordering or adding new method
- Changes the behavior of original model, won't be saved in DB
- Proxy model will also operate on the base model
class Person(models.Model):
pass
class MyPerson(Person):
class Meta:
proxy = True
ordering = ["last_name"]
def do_something(self):
pass
Manage.py file is automatically created when a new project is created & is used for administrative tasks.
It is more like django-admin but also sets DJANGO_SETTINGS_MODULE for project settings(which db, middleware)
Command | Function | Example |
---|---|---|
runserver | Starts a lightweight dev server on local machine on port 8000 and ip 127.0.0.1. Automatically reloads code on each HTTP request. Can provide local machine ip to make project accessible to other users. | $ python manage.py runserver 192.168.1.110:8000 |
check | Check apps for common problems | $ python manage.py check appname |
dumpdata | Returns all data within all apps on shell. Can be exported to a json, also can be exported for an app, all tables or a single table. | $ python manage.py dumpdata appname && python manage.py dumpdata appname > appname.json && python manage.py dumpdata appname.model_class_name |
loaddata | Loads named fixtures into DB | $ python manage.py loaddata fixture_name.json |
flush | Irreversibly destroy data currently in DB & return each table to an empty state | $ python manage.py flush |
makemigrations | Migrations are kind of a version control system for DB schema. makemigrations pack up the changes detected in the models into migration files(similar to a commit). New migrations are compared with the old migrations before creation. |
python $ python manage.py makemigrations --name changed_my_model appname1, appname2 |
migrate | Synchronizes the database state with the current set of models and migrations | $ python manage.py migrate changed_my_model --fake |
Fixtures are initial data for your database which can be used with manage.py loaddata command. Django will look for a fixtures folder in your app or the list of directories provided in the FIXTURE_DIRS in settings, and use its content instead.
- Model with a dependency is created first, say Teacher has foreign key to Person model, the Person model is created first
- Each app has migrations stored in migration file with class representation. Django.db.migrations.Migration is the migration class
- Each migration class lists dependencies(a list of mig this depends on) and operations(what was changed in models)
- These operations are then used to create the SQL which then makes the changes
- It is possible to write migration files manually without running the 'makemigrations' command
- Initial migrations are marked with an initial = True class attribute on the migration class
- If you have manually changed your db, Django won't be able to migrate changes | errors
- Migrations can be reverted by passing the migration number
$ python manage.py migrate app 002
- Migrations can also be named
$ python manage.py makemigrations --name changed_my_model your_app_label
Django creates a 'django_migrations' table in db with fields <ID | App | Name | Applied> when you apply any migration the first time. A new row is inserted for each migration that is applied or faked.
A project is whole idea behind your application and an app is the sub-module of that idea. Example- Users as a micro-service project with profile app for profile related management, authentication app and so on. In a general way, a project could be the whole website and an app could be functionalities it offers, like request app, result app, frontend app and so on.
- Create the project
$ django-admin.py startproject project_name
- Update your project settings in ./settings.py like installed_apps, middlewares, db, project variables & so on.
- Validate installment | use Django dev test server
- Version control project, creates requirements.txt
- Create the app
$ python manage.py startapp app_name
- Add app into project settings.py installed_apps
- Make migrations & migrate models
Squashing is the act of reducing an existing set of many migrations down to one (or sometimes a few) migrations which still represent the same changes. Django takes all the operations from the migrations, puts them in sequence and optimizes them.
- Create model and Delete model --> No operation
- You can name squashed migrations
python manage.py squashmigrations myapp --squashed-name squashed_migration_01
- All migration files must be deleted after a squashed migration(leaving the squashed one)
- Update dependencies of deleted migrations to squashed migration.
- Remove 'replaces' from squashed migration file. 'replaces' is what makes it a squashed migration
When to use which one?
Triggers and all
Use of two setting files.
Method | CRUD | Entire Collection | Specific Item |
---|---|---|---|
GET | Read | 200(OK) | 200(OK), 404(Not Found) |
POST | Create | 201(Created) | 404(Not found), 409(Conflict) |
PUT | Update | 405(Method Not Allowed) | 200(OK), 204(No Content), 404(Not Found) |
PATCH | Modify | 405(Method Not Allowed) | 200(OK), 204(No Content), 404(Not Found) |
DELETE | Delete | 405(Method Not Allowed) | 200(OK), 404 (Not Found) |
- Informational 1XX
- Successful 2XX
- Redirection 3XX
- Client Error 4XX
- Server Error 5XX
Calling the same PUT request multiple times will always produce the same result. In contrast, calling a POST request repeatedly make have side effects of creating the same resource multiple times.