Django Basic
1 | # APP models.py |
After adding new models, run python manage.py makemigrations
, and then run python manage.py migrate
Register models
1 | # APP admin.py |
JSON Response
1 | # APP views.py |
Class based View with Json
Reusable Mixin
1 | # PROJECT mixins.py |
1 | # use Mixin class |
[Caption] Why do we need to add as_view()
in urls pattern?
Answer: https://stackoverflow.com/questions/31491028/django-generic-views-based-as-view-method
The as_view()
function returns a callable view that takes a request
and then returns an response
. It’s the main entry-point in request-response cycle in case of generic views. And this is required in the Class Based Views in Django.
Data Serialization
Data Serialization is to convert the data into a dictionary or into a different data structure.
1 | from django.core.serializers import serialize |
We can put the serializer into models
models.Manager
: A Manager is the interface through which database query operations are provided to Django models. At least one Manager exists for every model in a Django application.
Adding extra Manager methods is the preferred way to add “table-level” functionality to your models. (For “row-level” functionality – i.e., functions that act on a single instance of a model object)
A custom Manager method can return anything you want. It doesn’t have to return a QuerySet.
See: https://docs.djangoproject.com/en/2.1/topics/db/managers/
1 | # APP models.py |
In the views.py
1 | class SeiralizedListView(View): |
OR models.py
1 | class UpdateQuerySet(models.QuerySet): |