Django and DRF
302 subscribers
98 photos
39 files
161 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
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.
👍3🔥3
🔌 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
9👍8
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 👌
🫡51
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'),
]
Lesson?
You don’t need 17 microservices when you can write clean Django apps with clear domain boundaries. Monoliths scale just fine if you don’t treat them like spaghetti.

Link to the article
👍1