The Django documentation which is the best source for learning and reading about it.
Django has built-in support for GIS data. You can find more about it here (for Django 4.1):
Writing REST API in a Django application using Django REST Framework is awesome, following links are the important concepts of the DRF (Django REST Framework):
Sometimes it is better in DRF to read its code because its documentation is not complete:
Semi-automatic swagger documentation for the REST APIs:
For doing upgrade, you can use:
Database Optimization
Django’s database layer provides various ways to help developers get the most out of their databases.
As general programming practice, this goes without saying. Find out what queries you are doing and what they are
costing you. Use QuerySet.explain()
to understand how specific QuerySet
s are executed by your database.
To avoid performance problems, it is important to understand:
- That
QuerySet
s are lazy. - When they are evaluated.
- How the data is held in memory.
Use iterator()
, When you have a lot of objects, the caching behavior of the QuerySet
can cause a large amount of memory to be used. In this case, iterator()
may help.
Dataclasses
Using data-classes to define request and response in Django REST Framework. There are cases in which your request or response is not a model, in those cases you can define them as a dataclass using the following library.
Using the library instead of 😔:
you can write 😍:
Django Filters
Having reusable filters for models in Django REST Framework with Django-filter. These filters help you to write viewsets easier and give client developers vast choices in getting the data.
inspectdb
There are cases in which you already have the database and want to describe it using Django models:
Models
A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.
The basics:
- Each model is a Python class that subclasses
django.db.models.Model
. - Each attribute of the model represents a database field.
- With all of this, Django gives you an automatically-generated database-access API; see Making queries.
Use OuterRef
when a queryset in a Subquery
needs to refer to a field from the outer query or its transform. It acts like an F
expression except that the check to see if it refers to a valid field isn’t made until the outer queryset is resolved.
select_related()
Returns a QuerySet
that will follow foreign-key relationships, selecting additional related-object data when it executes its query. This is a performance booster which results in a single more complex query but means later use of foreign-key relationships won’t require database queries.
in_bulk()
Takes a list of field values (id_list
) and the field_name
for those values, and returns a dictionary mapping each value to an instance of the object with the given field value.
If a list isn’t provided, all objects in the queryset
are returned.