Django and DRF

Description
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
We recommend to visit

Community chat: https://t.me/hamster_kombat_chat_2

Website: https://hamster.network

Twitter: x.com/hamster_kombat

YouTube: https://www.youtube.com/@HamsterKombat_Official

Bot: https://t.me/hamster_kombat_bot

Last updated 1 month, 1 week ago

Your easy, fun crypto trading app for buying and trading any crypto on the market.
📱 App: @Blum
🤖 Trading Bot: @BlumCryptoTradingBot
🆘 Help: @BlumSupport
💬 Chat: @BlumCrypto_Chat

Last updated 7 months ago

Turn your endless taps into a financial tool.
Join @tapswap_bot


Collaboration - @taping_Guru

Last updated 1 month, 3 weeks ago

1 month, 3 weeks ago
  1. Pagination siz:

response = CustomResponse.success( message="Data retrieved successfully", data={"key": "value"} )

  1. 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.
1 month, 3 weeks ago

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)

2 months, 2 weeks ago

System Design HandBook

3 months, 1 week ago

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

3 months, 1 week ago
Dioganal scailing - server resourse larini …

Dioganal scailing - server resourse larini va app instance larini paralel ravishda oshirish.
Manba: https://newsletter.systemdesign.one/p/whatsapp-engineering

3 months, 4 weeks ago
9 months, 2 weeks ago

Helpful Interview questions for Backend developers.

11 months, 1 week ago

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

11 months, 1 week ago

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.

We recommend to visit

Community chat: https://t.me/hamster_kombat_chat_2

Website: https://hamster.network

Twitter: x.com/hamster_kombat

YouTube: https://www.youtube.com/@HamsterKombat_Official

Bot: https://t.me/hamster_kombat_bot

Last updated 1 month, 1 week ago

Your easy, fun crypto trading app for buying and trading any crypto on the market.
📱 App: @Blum
🤖 Trading Bot: @BlumCryptoTradingBot
🆘 Help: @BlumSupport
💬 Chat: @BlumCrypto_Chat

Last updated 7 months ago

Turn your endless taps into a financial tool.
Join @tapswap_bot


Collaboration - @taping_Guru

Last updated 1 month, 3 weeks ago