Django and DRF
365 subscribers
98 photos
39 files
159 links
Purpose is sharing, spreading knowledge which is most related to the Django and Django Rest Framework(DRF)

You can find in here my
- Articles
- Projects with source codes
- Videos
and another useful stuffs
Download Telegram
Backend dasturchi bilishi kerak bo'lgan eng muhim 'qobiliyat' nima deb o'ylaysiz ?

Javoblarni komentariyada qoldiring, men uzimnikini kun yakunida qoldiraman.
Django and DRF
Backend dasturchi bilishi kerak bo'lgan eng muhim 'qobiliyat' nima deb o'ylaysiz ? Javoblarni komentariyada qoldiring, men uzimnikini kun yakunida qoldiraman.
Men uchun eng muhim part bu Database Structurasi ya'ni uni qurish, Design qilish.

Chunki bu qismni skip qilib ketish oqibatida kelajakda ko'p disadvantage tomonlarini kurishimiz mumkin.

1. Kod Trash ga aylanishi aniq I think so.

2. Ma'lumotlarni optimal tarzda database ga saqlash, olish, o'chirish, yangilash(CRUD) ni tezligini bir necha barobarga oshiradi.

3. Ortiqcha keraksiz querylarni yozishdan qutilish.

Design Tayyor boo'lgandan so'ng Mazza qilib api chiqarishga o'tsez bo'ladi 😂

Tepada yangi boshlayotgan loyihamizni Database Design ni, bugungi qilgan ishim shu bo'ldi🤓

Kamchiliklar bo'lsa yoki qo'shimcha qo'shmoqchi bo'lsangiz komentariyada fikringizni qoldiring 👇🏻
How to add multiple database in Django framework.pdf
7.9 MB
Bitta Django Loyihada qanday qilib bir nechta Database lardan foydalanish mumkin ?

#edited
pre-commit larni ishlatgan holda kodizni bir nechta Tekshiruvlardan o'tkazib toza 🧹, missed xatoliklardan fix qilib 🥺, refactoring qilish 🥶 va black,flake8,isort larni ishlatib vanihoyat yaratilmagan migratsiyalar qolib ketgan bo'lsa😢 ularni ham tekshirib Testlardan🧪 o'tkazib, coverage ni ham aniqlab ko'rgandan so'ng commit bo'lganini ko'rsangiz mazza qivorasiz 😎, ishonavering 😁

pre-commit haqida nimalar bilasiz, batafsil post qilishimni hohlaysizmi ?

leave your thoughts in the comments 👇🏻
Django Rest Framework orqali API lar chiqarganimizda ularni frontend dasturchilarga tushunarli tarzda yetkazishimiz uchun Dokumentatsiya yozishimiz kerak bo'ladi.

Shu documentatsiyani yozish uchun DRF da 2 ta popular packagelar bor drf-spectacular va drf-yasg,
Savol: Ikkalasini kotta farqi nimada ?, qanday advantage, disadvantage lari bor ?
𝐖𝐡𝐚𝐭 𝐢𝐬 𝐂𝐨𝐧𝐭𝐞𝐧𝐭 𝐃𝐞𝐥𝐢𝐯𝐞𝐫𝐲 𝐍𝐞𝐭𝐰𝐨𝐫𝐤 (𝐂𝐃𝐍)?

A CDN is a distributed network of servers strategically located across various geographical locations to deliver web content, such as images, videos, and other static assets, more efficiently to users.

The primary purpose of a CDN is to reduce latency and improve the overall performance of web applications by serving content from the server nearest to the user. CDNs can also help improve reliability, availability, and security of web applications.

𝐁𝐞𝐧𝐞𝐟𝐢𝐭𝐬 𝐨𝐟 𝐮𝐬𝐢𝐧𝐠 𝐚 𝐂𝐃𝐍
CDNs play a crucial role in enhancing the performance, reliability, and security of modern web applications. Here are the top benefits of using CDNs:

🔹1. Reduced latency: By serving content from geographically distributed edge servers, CDNs reduce the time it takes for content to travel from the server to the user, resulting in faster page load times and improved user experience.

🔹2. Improved performance: CDNs can offload static content delivery from the origin server, freeing up resources for dynamic content generation and reducing server load. This can lead to improved overall performance for web applications.

🔹3. Enhanced reliability and availability: With multiple edge servers in different locations, CDNs can provide built-in redundancy and fault tolerance. If one server becomes unavailable, requests can be automatically rerouted to another server, ensuring continuous content delivery.

🔹4. Scalability: CDNs can handle sudden traffic spikes and large volumes of concurrent requests, making it easier to scale web applications to handle growing traffic demands.

🔹5. Security: Many CDNs offer additional security features, such as DDoS protection, Web Application Firewalls (WAF), and SSL/TLS termination at the edge, helping to safeguard web applications from various security threats.

Reference: Introduction to CDN: https://lnkd.in/gwygXpcR

CDN haqida yana qanday +, - larini bilasiz ?
Django loyihasiga ElasticSearch implementatsiya qilmoqchi bo'lganlar uchun
1. https://django-elasticsearch-dsl-drf.readthedocs.io
2. https://django-elasticsearch-dsl.readthedocs.io/en/latest/

ElasticSearch - bu qidiruv tizmi uchun eng optimal variantlardan biri va loyihangizda live-search ni qullamoqchi bo'lsangiz juda qo'l keladi.

o'rganish uchun:
1. https://testdriven.io/blog/django-drf-elasticsearch/
2. https://www.udemy.com/course/elasticsearch-complete-guide/

bu haqida batafsil post qilaman.
Django Optimization
1. Use Advanced Expressions (Q, F, When, Case, ..)
2. Use django debug-toolbar to analyze and optimize each api or function
3. Caching data for not getting again
4. Use annotate(), aggregate() functions
5. Implement select_related(), prefetch_related() instead of increasing query's count
6. Use count() not len()
7. Avoid retrieving data which you don't need: use values_list(), values()
8. Use bulk_create() to create multiple entries
9. Use foreignkey values directly: book.user.id , book.user_id
10. Use Celery to do tasks using queues
....
qolganini commentda davom ettirishingiz mumkin 😊

Optimization haqidagi videolar to'plami: https://youtube.com/playlist?list=PLmSbOYZOHCRoNruZA0prIvOVu2mv3JzFm&feature=shared
Backend Expert Guide.pdf
6.9 MB
Helpful Interview questions for Backend developers.
Django dagi mavjud signallar

request_started - bu signal HTTP request proccesing eng boshlangich qismida ishlaydi, ya'ni Views, Middlewares dan ham oldin.

Yasalishi:
from django.core.signals import request_started
from django.dispatch import receiver

@receiver(request_started)
def my_callback(sender, **kwargs):
print("Request started!")


Foydalanish uchun caselar:
1. Logging
2. Performance Monitoring

more
Dioganal scailing - server resourse larini va app instance larini paralel ravishda oshirish.
Manba: https://newsletter.systemdesign.one/p/whatsapp-engineering
Q: What architecture is the best to build fastest app that can handle high traffic , has optimized code style, works on low cost

A: Each architectural pattern solves a specific problem.

Yet there’s no silver bullet.

So the correct solution depends on your needs and scale.

#system_design
1738339882543.pdf
11.3 MB
System Design HandBook
Respone structura muammosi ko'pchilikda bo'lsa kerak ya'ni frontend dasturchi bilan kelisholmaslik.

Frontendchi: "Nega hamma api larda bir xil response kelmaydi ? 😢"
Backendchi: "🙊..."

Buni xal qilish yo'li frontchi bilan bitta structuraga kelishib olishlik va hamma api larga shuni joriy qilish.

Buni biz django da qilishimiz uchun bitta class yaratib unda success va error holatlar uchun response tayyorlab beradigan metod yozib olamiz.
Shu metodlarni ichida kelishilgan structura asosida response qaytaramiz. Va bu classni view metodlarda foydalanamiz.

Quyidagi example da kurishingiz mumkin:

1. Class Kurinishi
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response

class CustomResponse:

@staticmethod
def _paginate_data(data, request, view, page_size: int = 10):
"""
Helper method to paginate the provided queryset.

Args:
data: The queryset or list to paginate.
request: The request object.
view: The view object.
page_size: The number of items per page.

Returns:
A tuple containing the paginated data and pagination details.
"""
paginator = PageNumberPagination()
paginator.page_size = page_size
paginated_data = paginator.paginate_queryset(data, request, view=view)

pagination_details = {
"count": paginator.page.paginator.count,
"next": paginator.get_next_link(),
"previous": paginator.get_previous_link()
}
return paginated_data, pagination_details

@staticmethod
def success(message: str, data=None, paginate: bool = False, request=None, view=None, page_size: int = 10) -> Response:
"""
Constructs a success response with optional pagination.

Args:
message: The success message.
data: The data to include in the response.
paginate: Whether to paginate the data.
request: The request object (required for pagination).
view: The view object (required for pagination).
page_size: The number of items per page.

Returns:
A Response object with the success message and optional data.

Raises:
ValueError: If `paginate` is True but `request` or `view` is missing.
"""
response = {
"status": "success",
"message": message,
}

if data is not None:
if paginate:
if request is None or view is None:
raise ValueError("Request and view are required for pagination.")
paginated_data, pagination_details = CustomResponse._paginate_data(data, request, view, page_size)
response["pagination"] = pagination_details
response["data"] = paginated_data
else:
response["data"] = data

return Response(response, status=200)

@staticmethod
def error(message: str, status_code: int = 400) -> Response:
"""
Constructs an error response.

Args:
message: The error message.
status_code: The HTTP status code for the error.

Returns:
A Response object with the error message.
"""
response = {
"status": "error",
"message": message,
}
return Response(data=response, status=status_code)

* bu yerda biz _paginate_data degan metod ham qushdik, agar biz qaytarmoqchi bo'lgan response pagianted bo'lib qaytish kerak bo'lib qolsa bu muammosiz amalga oshadi.
* paginator attribute ga uzimiz hohlagan Pagination class ni quyishimiz mumkin
* pagination qachon ishlaydi - qachonki paginate=True va data is not None bo'lganda


Foydalanish holatlari:
1. Pagination bilan:
return CustomResponse.success(message=_("Folders retreived successfully"), data=serializer.data, paginate=True, request=request, view=None, page_size=15)
Please open Telegram to view this post
VIEW IN TELEGRAM
2. Pagination siz:
response = CustomResponse.success(
message="Data retrieved successfully",
data={"key": "value"}
)

3. Error response holati:
response = CustomResponse.error(
message="Invalid input provided",
status_code=400
)

return CustomResponse.error(message=_("Folder not found"), status_code=404)


* bu yerda keltirilgan structura misol uchun, aslida o'zingiz hohlaganday qilishingiz mumkin.
🔌 WSGI — Web Server Gateway Interface nima va u qanday ishlaydi?

WSGI — bu Python ilovasi va web server o‘rtasidagi interfeys. U Python ilovalarini mustaqil web serverlar bilan ulash uchun yaratilgan standart (PEP 3333).

🧐 Nega WSGI kerak?
Python’ning o‘zi HTTP so‘rovlarni to‘g‘ridan-to‘g‘ri qabul qila olmaydi. Web serverlar esa so‘rovlarni olish, load balancing, static fayllarni berish kabi ishlarni bajaradi.
Ammo ular Python kodini to‘g‘ridan-to‘g‘ri ishlata olmaydi. WSGI bu ikkisini bog‘lovchi ko‘prik vazifasini bajaradi.

📥 So‘rovdan Javobgacha qanday oqim bo‘ladi?
1. Foydalanuvchi brauzer orqali HTTP so‘rov yuboradi.
2. Web server (masalan, Nginx yoki Apache) bu so‘rovni oladi.
3. U so‘rovni Gunicorn yoki uWSGI kabi WSGI serveriga uzatadi.
4. WSGI server application(environ, start_response) funksiyasini chaqiradi.
5. Ilova start_response() orqali status va header qaytaradi va natijani iterable sifatida jo‘natadi.
6. Natija web server orqali foydalanuvchiga yuboriladi.


🔧 WSGI nimani yengillashtiradi?
- Ilova va serverni mustaqil qiladi: istalgan Python framework (Django, Flask) istalgan serverda (Gunicorn, uWSGI) ishlaydi.
- Performance va scalability uchun optimallashtirishni web serverga topshiradi.
- Bu standart bo‘lishi tufayli, loyihalar orasida moslashuvchanlikni oshiradi.


🔍 Muqobil variantlar bormi?
Ha. ASGI — WSGI ning asynchronous versiyasi bo‘lib, real-time (chat, WebSocket) ilovalar uchun ishlatiladi. Ammo, oddiy HTTP ilovalar uchun WSGI hali ham keng qo‘llaniladi.


📚 Manbalar:
- https://builtin.com/data-science/wsgi
- https://peps.python.org/pep-3333/

👉 Bizni kuzatishda davom eting keyingi postimizda ASGI haqida gaplashamiz: @yakubovdeveloper
Django da ko'p qilinadigan xatolar #1

Model save() metodida quyidagi holatni ko'p ishlatish


class User(models.Model):
def save(self, *args, **kwargs):
if not self.pk:
send_welcome_email(self.email)
super().save(*args, **kwargs)

yaxshidek ko'rinadi ammo unday emas, agar kodda kimdur bulk_create() ishlatgan holda 10k user yaratgan bo'lsa 10k email yuborlmay qoladi🫠

shuning uchun bunaqa ishlarni odatda signals da yoki user save qilayotgan joyda task orqali hal qilish yaxshi yechim 👌
https://medium.com/@pesarakex/how-i-reduced-a-3gb-django-table-query-from-12s-to-200ms-with-just-3-indexes-eec82e99cec9

Django model:

class Record(models.Model):
user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
status = models.CharField(max_length=50)
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
indexes = [
models.Index(fields=['user', 'status'], name='idx_record_user_status'),
models.Index(fields=['-created_at'], name='idx_record_created_at'),
models.Index(fields=['user', 'status', '-created_at'], name='idx_record_user_status_created'),
]