React Native Init vs Expo 2023: What’s the Difference?
3rd November 2023Django: 10 Common Anti-Patterns to Avoid 🦄
18th November 2023Hello Coders! As announced on the official Django site, the Django 5.0 Version is out. The changes provided in this version are listed below.
Curious minds and cutting-edge developers can test the new features by using the latest Django 5.x release:
$ pip install Django==5.0a1
✅ Database-computed default values
The new field added,
Field.db_default
, parameter sets a database-computed default value.Here is the official sample:
from django.db import models from django.db.models.functions import Now, Pi class MyModel(models.Model): age = models.IntegerField(db_default=18) created = models.DateTimeField(db_default=Now()) circumference = models.FloatField(db_default=2 * Pi())
✅ Database generated model field
The new
GeneratedField
allows the creation of database-generated columns.
Here is the official sample:
from django.db import models from django.db.models import F class Square(models.Model): side = models.IntegerField() area = models.GeneratedField(expression=F("side") * F("side"), db_persist=True)
✅ Options for declaring field choices in Django
Field.choices
(for model fields) and ChoiceField.choices
(for form fields) allow for more flexibility when declaring their values. In previous versions of Django, choices
should either be a list of 2-tuples, or an Enumeration types subclass, but the latter required accessing the .choices
attribute to provide the values in the expected form
Django 5.0 adds support for accepting a mapping or a callable instead of an iterable, and also no longer requires .choices
to be used directly to expand enumeration types.
Here is the official sample:from django.db import models
from django.db import models
Medal = models.TextChoices("Medal", "GOLD SILVER BRONZE")
SPORT_CHOICES = { # Using a mapping instead of a list of 2-tuples.
"Martial Arts": {"judo": "Judo", "karate": "Karate"},
"Racket": {"badminton": "Badminton", "tennis": "Tennis"},
"unknown": "Unknown",
}
def get_scores():
return [(i, str(i)) for i in range(10)]
class Winner(models.Model):
medal = models.CharField(..., choices=Medal) # Using `.choices` not required.
sport = models.CharField(..., choices=SPORT_CHOICES)
score = models.IntegerField(choices=get_scores) # A callable is allowed.
✅ Other minor changes in Django
django.contrib.admin
– jQuery is upgraded to 3.7.1.django.contrib.auth
– The new asynchronous functions are now provided, usinga
prefix:django.contrib.auth.aauthenticate()
,aget_user()
,alogin()
,alogout()
, andaupdate_session_auth_hash()
.- Asynchronous views — Under ASGI,
http.disconnect
events are now handled. This allows views to perform any necessary cleanup if a client disconnects before the response is generated.
✅ In Summary
Django is a high-level Python web framework that simplifies and accelerates web development by providing a robust set of tools, libraries, and conventions. It emphasizes rapid development, clean and pragmatic design, and follows the “Don’t Repeat Yourself” (DRY) principle.
Since its first release in 2003, Django has been continuously improved by a super-active community where all the new features are discussed, voted, implemented, and later released.
With built-in features for handling databases, authentication, templating, and more, Django empowers developers to build scalable and secure web applications efficiently.
Django-framework ’s history is characterized by its commitment to simplicity, best practices, and robustness. It has evolved to adapt to the changing landscape of web development while maintaining a strong focus on developer productivity and code quality.
Today, Django continues to be a widely used and respected web framework, with an active community of developers and a rich ecosystem of packages and extensions.
Learn more about top-10-python-libraries
Know more about website speed performance here