![]() |
Django content types framework enables flexible relationships between models through generic foreign keys, increasing database scalability. This article will show how to implement a content types framework in Django. What is the ContentTypes Framework in Django?The ContentTypes framework in Django provides a way to create relationships between models dynamically. Traditionally, when defining a ForeignKey or ManyToManyField in a model, you specify the target model directly. However, with the ContentTypes framework, you can create relationships without explicitly referring to the target model. Instead, the relationship is established using content type IDs and object IDs.
Home | tagged item
Home | book
Home | author
Sessions | session
Content Types | content type
Authentication and Authorization | user
Authentication and Authorization | group
Authentication and Authorization | permission
Administration | log entry Implementation of Content types Framework in DjangoBelow is the Implementation of Content types Framework in Django in Python: Starting the Project FolderTo start the project use this command django-admin startproject core
cd core To start the app use this command python manage.py startapp home 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",
"home",
] File Structure ![]() File Structure Setting Necessary Filesmodels.py : Django code shows the usage of the content types framework, enabling dynamic relationships between models. The
views.py : Django view function retrieves all authors, books, and tagged items from the database and passes them to the
Creating GUIindex.html : HTML template displays a list of authors, books, and tagged items retrieved from the Django view. It iterates through each object and presents relevant information, such as author names, book titles, and tagged item details like content type and object ID.
core/urls.py : Below, are the urls.py file of project.
home/urls.py : Below is the urls.py file of app.
admin.py:Here we are registering our models.
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 ![]() The contenttypes framework in django Video Demonstration |
Reffered: https://www.geeksforgeeks.org
Django |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |