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
response = CustomResponse.success(
message="Data retrieved successfully",
data={"key": "value"}
)
response = CustomResponse.error(
message="Invalid input provided",
status\_code=400
)
return CustomResponse.error(message=\_("Folder not found"), status\_code=404)
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:
```
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)
```
Foydalanish holatlari:
1. Pagination bilan:
return CustomResponse.success(message=\_("Folders retreived successfully"), data=serializer.data, paginate=True, request=request, view=None, page\_size=15)
System Design HandBook
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.
Dioganal scailing - server resourse larini va app instance larini paralel ravishda oshirish.
Manba: https://newsletter.systemdesign.one/p/whatsapp-engineering
JWT Logout in Django Rest Framework
https://medium.com/django-rest/logout-django-rest-framework-eb1b53ac6d35
Helpful Interview questions for Backend developers.
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
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.
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