Learn Python Coding
40.9K subscribers
712 photos
37 videos
24 files
524 links
Learn Python through simple, practical examples and real coding ideas. Clear explanations, useful snippets, and hands-on learning for anyone starting or improving their programming skills.

Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
• Fetch related ForeignKey objects in the same query.
entries = Entry.objects.select_related('author').all()

• Fetch related ManyToManyField objects in a separate efficient query.
entries = Entry.objects.prefetch_related('tags').all()

• Load only specific model fields.
entries = Entry.objects.only('headline')

• Defer loading of specific model fields.
entries = Entry.objects.defer('body_text')

• Execute raw, unmanaged SQL.
authors = Author.objects.raw('SELECT * FROM myapp_author')

• Get results as a list of tuples.
Entry.objects.values_list('headline', 'pub_date')


XV. Transactions

• Import the transaction module.
from django.db import transaction

• Run a block of code within a database transaction.
with transaction.atomic():
# All database operations here are either committed together or rolled back.
author.save()
entry.save()


XVI. Managers & Model Methods

• Create a custom Manager for common queries.
class PublishedEntryManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='published')

• Add a custom method to a QuerySet via its Manager.
Entry.objects.get_queryset().by_author("John Doe")

• Add a custom method to a model for object-specific logic.
class Entry(models.Model):
#...
def is_recent(self):
return self.pub_date > timezone.now() - timedelta(days=1)


#Python #Django #ORM #Database #Backend

━━━━━━━━━━━━━━━
By: @DataScience4 ✨
❤1
This media is not supported in your browser
VIEW IN TELEGRAM
❤️ Architecture Patterns — an informative repository on backend architecture in Python!

Here, they excellently demonstrate how to properly separate application logic, work with complex architecture, build a scalable backend, and maintain a codebase in an adequate state as the project grows. Instead of dry theory, the authors gradually build a full-fledged application and show how the architecture evolves as the project grows.

I'll leave a link: https://github.com/cosmicpython/book

#Python #Backend #Architecture #Coding #DevCommunity #OpenSource

✨ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
❤2
Cheat sheet on Python Frameworks:

Django: A full-featured web framework with built-in ORM, admin panel, and security features.

Flask: A lightweight microframework with a minimal set of features and high flexibility.

ORM & Admin: Built-in to Django, but need to be connected separately in Flask.

Security: Django has built-in security mechanisms, while in Flask, they need to be configured manually.

Testing: Django offers built-in testing tools, while Flask relies on third-party libraries.

Use Cases: Django is suitable for large and complex projects, while Flask is better for small applications, APIs, and prototypes.

#Python #WebDev #Django #Flask #Backend #Programming

✨ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
❤7
🚀 Django 6.1: Loading related data on demand, cascading deletion handled by the database, and a new email configuration.

The release was made on August 5th. Three major changes:

1. New Field Loading Modes 📂

FETCH_PEERS retrieves data for related objects from the same QuerySet when accessing an un-loaded field. This helps combat the N+1 problem:
from django.db import models

books = Book.objects.fetch_mode(models.FETCH_PEERS)

for book in books:
print(book.author.name)

In this example, Django executes two queries: one for the books and one for their authors.

The FETCH_RAISE mode raises an exception instead of performing an implicit query – useful for detecting unexpected database accesses.

2. Deletion at the Database Level 🗑

For ForeignKey.on_delete, the options DB_CASCADE, DB_SET_NULL, and DB_SET_DEFAULT are now available. The logic is handled directly by the database using SQL ON DELETE.

Note: DB_CASCADE does not trigger pre_delete and post_delete signals.

3. Multiple Email Backends 📧

The MAILERS configuration allows you to define different connections using a dictionary, and you can select the desired connection when sending emails using the using parameter.

Before upgrading, check your database versions: you need PostgreSQL 15+, MySQL 8.4+, MariaDB 10.11+, or SQLite 3.37+.

🔗 https://www.djangoproject.com/weblog/2026/aug/05/django-61-released/

#Django #Python #WebDevelopment #Django61 #Backend #TechNews

✨ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
❤2