![]() |
In web development, Django is a framework that helps to build awesome websites. One of the main features of Django is dynamic model fields so we will discuss what we mean by dynamic model fields and how to use them in Django. In this article, we will read about the dynamic model fields in Django. What are Django Dynamic Model Fields?Dynamic Model fields in Django let you create a model that can change or grow over time when you use dynamic model fields. Dynamic model fields are like a magic box; you can put anything you want and the box adjusts to fit all. For example, you are making a digital library where people can add their types of books. Some might want to add cookbooks, others might add novels and some might even comics so how can you handle all these different types without making a mess? that’s all we can handle using Django’s dynamic model field. Django Dynamic Model FieldsBelow are the examples of Django dynamic model fields in Python: Starting the Project FolderTo start the project use this command django-admin startproject demo
cd demo To start the app use this command python manage.py startapp firstt Now add this app to the ‘settings.py’ INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"firstt",
] File Structure![]() Example 1: Generic Relations in Dynamic Model Fields
firstt/models.py : To understand the dynamic field models using Generic Relations. I take examples of comments on videos and photos. So I have created a model of photos, videos and comments. In the below code, comment models have a generic relation with photos and videos.
firstt/views.py : After creating a model I have created a view for testing generic relations. In views, I have created function generic function and in generic function, views I have added one photo and video then added a comment photo and video after this, I filtered based on content and showed the comments for photos and videos.
firstt/urls.py : After adding the URL of the generic function in urls.py
After running of local server(http://127.0.0.1:8000/). I have hit this URL (http://127.0.0.1:8000/generic/). Example 2: JSON Fields (Dynamic Model Fields)
Let’s say you’re developing a platform where users can create custom forms, each with different fields based on their needs. Instead of defining fixed fields for each form, you can use a JSON field to store the form data flexibly. To create this platform we create the first model ‘CustomForm’. firstt/models.py : In the below code, I have created the model CustomForm. In CustomForm Field add name field and form_data field. form_data field is JSONField so in this field, you can add different fields of different users. For testing these fields I have created a view.
firstt/views.py : This code dynamically generates a custom form using the provided form_data, creates a CustomForm object with this data, and then returns the form data as an HTTP response
firstt/urls.py : In this example, I have added data from three persons from different fields.
admin.py : I have register all models in admin portal.
After running of local server(http://127.0.0.1:8000/). I have hit this URL (http://127.0.0.1:8000/json/). In above all example code we need to make migration on code for those follow the below steps, and run in terminal below command. Deployment of the ProjectRun these commands to apply the migrations: python3 manage.py makemigrations
python3 manage.py migrate Run the server with the help of following command: python3 manage.py runserver Output |
Reffered: https://www.geeksforgeeks.org
Django |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |