Posts

Showing posts from September, 2024

Mastering Django ORM Update Queries — From Basics to Pro Level

Updating data properly is just as important as selecting data. In this guide, you will learn how to update records correctly in Django ORM — from basic .save() usage to pro techniques like atomic updates, bulk updates, F expressions, and best practices! 📚 1. Basic Update Using .save() Fetch and modify a single object user = NewUser.objects.get(pk=1) user.email = 'newemail@example.com' user.save() ⚡ 2. Updating Specific Fields with .save(update_fields=[...]) user = NewUser.objects.get(pk=1) user.email = 'newemail@example.com' user.save(update_fields=['email']) 🎯 3. Updating Multiple Records at Once Using .update() NewUser.objects.filter(user_type=2).update(is_active=False) 🧠 4. .save() vs .update() — Key Differences Aspect .save() .update() Works on Single object QuerySet (multiple) Triggers signals? ✅ Yes ❌ No Auto-updates auto_now fields? ✅ Yes ❌ No Custom logic inside save() runs? ✅ Yes ❌ No Speed for bulk ❌ Slow ✅ Fast 🔥 5. Upda...

Understanding Time and Space Complexity

Image
When writing code, one of the key considerations is efficiency—how quickly the program runs and how much memory it uses. These two concepts are measured by time complexity and space complexity . Understanding how different operations affect time and space complexity is crucial for optimizing algorithms. 1. Time Complexity Time complexity is a way to represent the amount of time an algorithm takes to complete as a function of the input size. The most common time complexities include: O(1) Constant Time : The operation takes the same amount of time, no matter the input size. O(log n) Logarithmic Time : The operation’s time grows logarithmically as input increases (e.g., binary search). O(n) Linear Time : The operation time grows linearly with the input size. O(n²) Quadratic Time : Time grows quadratically as the input size increases (e.g., nested loops). O(2ⁿ) Exponential Time : Time doubles with each additional input (e.g., some recursive algorithms). ...

Understanding Fibonacci: A Developer’s Perspective

Image
The Fibonacci sequence is one of the simplest and most fascinating concepts in mathematics. Its presence in nature, computer algorithms, and problem-solving makes it an ideal case study for understanding various programming concepts. In this blog, we will explore Fibonacci from both a mathematical and a developer's standpoint, covering recursive vs iterative approaches , the importance of time and space complexity , and the implications of shallow vs deep copying along with the behavior of primitives and objects . 1. The Mathematics Behind Fibonacci The Fibonacci sequence starts with 0 and 1, with each subsequent number being the sum of the two preceding ones. Mathematically, this is expressed as: F(n) = \begin{cases} 0 & \text{if } n = 0, \\ 1 & \text{if } n = 1, \\ F(n-1) + F(n-2) & \text{if } n \geq 2 \end{cases} F(0) = 0 F(1) = 1 F(2) = 1 F(3) = 2 and so ...