Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Django
  • Views
  • Model
  • Template
  • Forms
  • Jinja
  • Python SQLite
  • Flask
  • Json
  • Postman
  • Interview Ques
  • MongoDB
  • Python MongoDB
  • Python Database
  • ReactJS
  • Vue.js
Open In App
Next Article:
Using UUID As The Primary Key In Django Models
Next article icon

How Do we Reference a Django Settings Variable in My models.py?

Last Updated : 09 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

One common requirement in Django projects is accessing the settings variables defined in the settings.py file in different apps. We can use the settings variables anywhere in our project. We only need to add an import statement 'from django.conf import settings' and refer to the variable using the settings.<Variable Name>. In this article, we will show you how to reference a Django settings variable in your models.py file.

Defining Settings Variables in settings.py

In your settings.py file, add a variable of your choice and assign a value you want to use in your models.py file.

For example, let's say, you want to set a default value for a model field defined in the settings.py file.

settings.py

Python
# ...  MY_DEFAULT_VALUE = 'This is a custom setting value'  # ... 

Models.py

Now, in the models.py file of your application, import settings from django.conf.

In this example, the setting_value field in MyModel is set to the value of MY_DEFAULT_VALUE from the settings.py file.

Python
# myapp/models.py  from django.db import models from django.conf import settings  class MyModel(models.Model):     name = models.CharField(max_length=100)     setting_value = models.CharField(max_length=255, default=settings.MY_CUSTOM_SETTING)          def __str__(self):         return self.name 

Register the Model in Admin

Let's register the MyModel in the admin.py file and to see your changes, you can go to the admin panel and create some instances.

admin.py

Python
from django.contrib import admin from .models import MyModel  admin.site.register(MyModel) 

Making Migrations

Run the following commands to create and apply migrations:

python manage.py makemigrations
python manage.py migrate

Creating a Superuser

Create a superuser to access the Django admin interface in you don't have one:

python manage.py createsuperuser

Run the Development Server

python manage.py runserver

Visit http://127.0.0.1:8000/admin in your browser, log in with the superuser credentials, and you will see the MyModel in the admin interface. And yes, we have successfully used the setting variables in the models.py file.

1



Next Article
Using UUID As The Primary Key In Django Models

K

kasoti2002
Improve
Article Tags :
  • Python
  • Python Django
Practice Tags :
  • python

Similar Reads

  • How to Manage Local vs Production Settings in Django?
    When developing a Django application, managing settings for different environments (local development, testing, staging, and production) is crucial for ensuring smooth deployment and security. You don't want the same settings (like debug mode or database credentials) for local development and produc
    5 min read
  • How to add Site Header, Site Title, Index Title in a Django Project?
    Automatic admin interface one of the most powerful parts of Django. Metadata is read from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization’s internal management tool. It’s not intend
    1 min read
  • How to use User model in Django?
    The Django’s built-in authentication system is great. For the most part we can use it out-of-the-box, saving a lot of development and testing effort. It fits most of the use cases and is very safe. But sometimes we need to do some fine adjustment so to fit our Web application. Commonly we want to st
    3 min read
  • Using UUID As The Primary Key In Django Models
    When building Django applications, we often rely on the auto-incrementing primary key (id) generated by the database. However, there are cases where using UUIDs (Universally Unique Identifiers) as primary keys can provide benefits such as security, uniqueness across distributed systems, and reduced
    4 min read
  • Python | Relational fields in Django models
    Prerequisite: Django models   Django models represent real-world entities, and it is rarely the case that real-world entities are entirely independent of each other. Hence Django supports relational databases and allows us to establish relations between different models. There are three types of rel
    4 min read
  • How to Read the Database Table Name of a Model Instance in Python Django?
    Each Django Model corresponds to a table in the database. Understanding how to access the database table name associated with a model instance is important for various tasks, such as debugging, logging, or, creating dynamic queries. This article will guide us through how to read the database table n
    3 min read
  • How to Rename Items in values() in Django?
    In Django, the values() method of a QuerySet is a powerful tool that allows us to return a subset of fields from the database. This is especially useful when we need only specific columns from a model and don't require the entire model instance. A noteworthy feature of the values() method is its abi
    3 min read
  • How do I run the pytest tests in Django?
    Are you someone who wants to ensure the stability and reliability of their application then testing Django is a very important step you need to perform. Not just this but pytest is a very popular testing framework for Python which provides a simple yet powerful way to write and run tests. For people
    4 min read
  • Set a Default Value for a Field in a Django Model
    Django is a powerful web framework for building web applications in Python. One common task in Django is setting up models, which are classes that represent database tables. Sometimes, it's useful to have default values for certain fields in these models. This article will guide you through creating
    4 min read
  • How to Extend User Model in Django
    Django’s built-in User model is useful, but it may not always meet your requirements. Fortunately, Django provides a way to extend the User model to add additional fields and methods. In this article, we'll walk through the process of extending the User model by creating a small web project that dis
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences