Member-only story

Introducing ‘GeneratedField’: A New Feature in Django 5.0

Sanjay Prajapati
Django Unleashed
Published in
3 min readNov 19, 2024

Django 5.0 introduces an exciting new feature: GeneratedField.

This long-awaited addition empowers developers to create fields with dynamic values directly in the database, reducing Python-side calculations and improving performance.

Let’s dive into this feature and see how it can streamline your Django projects! 🚀

🧐 What is GeneratedField?

GeneratedField is a database-computed field, meaning its value is automatically calculated by the database based on an expression involving other fields in the same model.

The database manages and updates the field without requiring any Python-side calculations.

🔑 Key Benefits:

  • Uses the ‘GENERATED ALWAYS’ SQL syntax for computed columns.
  • Reduces database query complexity.
  • Offloads computations from Python to the database.
  • Simplifies maintaining derived data.

💡How to Use GeneratedField

Here’s how you can define and use GeneratedField in your Django models:

Imagine you want to store a user’s full name, derived from their first and last names.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Django Unleashed
Django Unleashed

Published in Django Unleashed

Unleashing the Full Potential of Web Development

Sanjay Prajapati
Sanjay Prajapati

Written by Sanjay Prajapati

I'm on an unwavering quest for success through the craft of writing.

Responses (2)

Write a response

Thanks for sharing. Another limitation is that, as of now, it doesn't work with Foreign key relationships.

The GeneratedField in Django 5.0 is a significant step forward for efficient model design.

Thanks for sharing