Please enable JavaScript.
Coggle requires JavaScript to display documents.
Django (CONCEPTS (FORUM (Forms can validate data, and have helpful…
Django
CONCEPTS
-
MODEL
-
Inheritance
Multi-table (not recommended) :warning:
Multi-table inheritance create table for each class in the database
Abstract :check:
Abstract inheritance doesn’t create extra tables. In this case we don’t need to query multiple tables to get the result as the case was with multi-table inheritance.
create a Meta class in Model class and create abstract attr with True
-
-
SIGNALs
:question:Signal == Callback (kind of, we can register listeners)
FORUM
Forms can validate data, and have helpful functionality to create forms
Two types of Forms: standard, model-forms
Standards forms are free from models, good for things like external data validations
-
Validation, Custom Validation, clean_fieldname()
Form.clean () is called after all the validations are done
-
-
-
-
-
MIDDLEWARE
order of middleware defined in setting.py is important, as they run in that orders
there are 5 hooks for a middleware, 2 before view is called and 3 after view is called
We create a class and implement the hook functions, thats it :check:
MISC
+ve points
-
-
-
MVT design patter, T(kind of controller)
-
Debugger-tool :check:
For performance debugging use Django-debug-tool which adds a icon on all pages which shows the SQL query information, like how many quries ran, CPU time etc.
-
-
-
CHECKLIST :star:
-
run makemigration, migrate commands after modyfing Models
-
PYTHON
getattr(), we can set default value if attribute not found
Structure
project
Apps
-
-
-
-
static-files
-
in settings.py, create a variable STATICFILES_DIR = [os.path.join(BASE_DIR, 'static'),]
templates
-
create folders in the app-directory level: templates/<app name>/index.html(or any other html file). <app name> inside the templates folder so that django doesn’t confuse with appname at top level
models.py
keep ORM models in this file, create more files if needed
-
after making changes to models.py file create migrations
python manage.py makemigrations <model name>
python manage.py migrate # to make the changes
add new models to admin site, register the model:
from django.contrib import admin
admin.site.register(<Model from models.py>)
fixing plural name in Admin site for the app
create Meta class
class Meta:
verbose_name_plural = 'name'
-
-
manage.py is like django-admin, but specific to the project. When we create a project it will also create a manage.py file in the app folder
manage.py
manage.py is used to run things like: migrations, dev-server …
-
-
python manage.py createsuperuser --name=<name of user> --email=<email of user> # this superuser(staff user) can create more users and more things
-
-
settings.py
-
settings.py -> INSTALLED_APPS, contains all apps for this projects
settings.py -> MIDDLEWARE, have authentication modules which is dependent on session layer. Order in MIDDLEWARE is very important
-
mysql-support
-
go to settings.py -> DATABASE, update sqlite3 to mysql and specify database name which already exists. Add USER, PASSWORD, HOST, PORT in the settings
-
CODE
Model
-
Create: Model.objects.create(param1=value1, param2= ....)
filter
Model.objects.filter(condition1=<something>, condition2=...)
-
.objects is the Model manager, we can create our own Model manager methods which write specific query.
We can also create queryset in manager
-
Management scripts to load data :question:
it uses django's manage commands, good for loading data
objects.get
DoesnotExists, MultipleObjectsReturne exceptions could be raised :warning:
Template
Jinja
Loop: posts is a context which is passed to render function as dict whose key name is 'posts'
{% block content %}
{% for post in posts %}<h1> {{ post }} </h1>
{% endfor %}
{% endblock %}
-
Template tag may/maynot take argument to function, e.g pet.objects.all
pet.submittion_date|date:"M D Y"(here argument is after : followed by ""
-
URL
from django.conf.urls import url
urlpatterns = [
url(r'regex', view_function, name='name'),
url(r'^details/(?P<id>\d+)/$', view_function, name='name),
]
VIEWS
FUNCTION
def view_func(request, <inputfunction>):
...
return render()
CLASS based views :star:
-
if custom context needed for data, implement get_context_data()
-
MIXINs
We create normal class extending object and define functions (for some custom code)
and finally we call super().same_fuction() to pass the call.
this is based on MRO concept, nothing special
-
-
-