{"id":120137,"date":"2024-12-16T14:40:26","date_gmt":"2024-12-16T14:40:26","guid":{"rendered":"\/tutorials\/?p=120137"},"modified":"2025-12-18T22:45:12","modified_gmt":"2025-12-18T22:45:12","slug":"django-models-and-databases","status":"publish","type":"post","link":"\/my\/tutorials\/django-models-and-databases\/","title":{"rendered":"How to work with Django models and databases"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Django models are the heart of data management in any Django application. They define the structure of your database and serve as the foundation for creating dynamic, data-driven applications. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With Django\u2019s powerful object-relational mapping (ORM) system, working with models becomes seamless, enabling you to define, query, and manipulate data effortlessly. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s explore how Django models and databases work together to bring your application to life.<\/p>\n\n\n\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-what-are-django-models\">What are Django models?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Django, <strong>models<\/strong> define the structure of your database tables. They are Python classes that map directly to database tables, so you can interact with your data in a convenient, high-level way.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each model class represents a table, and each model attribute represents a field in that table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Django&#8217;s ORM system eliminates the need to write raw SQL queries, simplifying tasks like creating, updating, and managing database records.<\/p>\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-how-to-create-a-django-model\">How to create a Django model?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To create a model, you define a Python class inside the <strong>models.py<\/strong> file of one of your apps. Each attribute in the class represents a database field, and Django will automatically handle the underlying database structure when you run migrations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s an example of a simple model in Django:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># In your app's models.py file\n\nfrom django.db import models\n\nclass Book(models.Model):\n\n\u00a0\u00a0\u00a0\u00a0title = models.CharField(max_length=200)\n\n\u00a0\u00a0\u00a0\u00a0author = models.CharField(max_length=100)\n\n\u00a0\u00a0\u00a0\u00a0published_date = models.DateField()\n\n\u00a0\u00a0\u00a0\u00a0isbn = models.CharField(max_length=13, unique=True)\n\n\u00a0\u00a0\u00a0\u00a0def __str__(self):\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return self.title<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>title<\/strong> and <strong>author<\/strong> \u2013 these are character fields (<strong>CharField<\/strong>) with a maximum length.<\/li>\n\n\n\n<li><strong>published_date<\/strong> \u2013 this is a date field (<strong>DateField<\/strong>).<\/li>\n\n\n\n<li><strong>isbn<\/strong> \u2013 a character field (<strong>CharField<\/strong>) with a unique constraint (<strong>unique=True<\/strong>).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>__str__()<\/strong> method ensures that when you query for a book, it will return the book&#8217;s title as a string representation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" id=\"h-model-fields\">Model fields<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In Django, model fields define the data type you want to store in your database. Each field type specifies how the data will be stored and validated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below are some of the most common field types:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CharField<\/strong> \u2013 for short text (e.g. names).<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">name = models.CharField(max_length=100)<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>TextField<\/strong> \u2013 for long text (e.g. descriptions).<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">description = models.TextField()<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>IntegerField<\/strong> \u2013 for whole numbers.<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">age = models.IntegerField()<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>FloatField<\/strong> \u2013 for decimal numbers.<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">price = models.FloatField()<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>BooleanField<\/strong> \u2013 for <strong>True<\/strong> or <strong>False<\/strong> values.<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">is_active = models.BooleanField(default=True)<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>DateField<\/strong> \u2013 for storing dates.<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">birth_date = models.DateField()<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>DateTimeField<\/strong> \u2013 for storing both date and time.<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">created_at = models.DateTimeField(auto_now_add=True)<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ForeignKey<\/strong> \u2013 for creating a many-to-one relationship with another model.<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">user = models.ForeignKey(User, on_delete=models.CASCADE)<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ManyToManyField<\/strong> \u2013 for creating a many-to-many relationship between models.<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">tags = models.ManyToManyField(Tag)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" id=\"h-model-relationships\">Model relationships<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Model relationships define how models are connected to each other. Django provides three main types of relationships:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ForeignKey<\/strong> \u2013 defines a many-to-one relationship. One object in this model is linked to one object in another model (e.g., a post belongs to a user).<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">user = models.ForeignKey(User, on_delete=models.CASCADE)<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>OneToOneField<\/strong> \u2013 defines a one-to-one relationship. Each object in one model is linked to exactly one object in another model (e.g., a user has one profile).<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">profile = models.OneToOneField(Profile, on_delete=models.CASCADE)<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ManyToManyField<\/strong> \u2013 defines a many-to-many relationship. Many objects of one model can be linked to many objects of another model (e.g., a post can have multiple tags, and each tag can belong to multiple posts).<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">tags = models.ManyToManyField(Tag)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">These relationships let you create connections between models and establish links between data in your database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-adding-the-model-to-the-database\">Adding the model to the database<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once you\u2019ve created your model, Django needs to translate it into a database table. This is done through <strong>migrations<\/strong>, which are files that Django uses to apply changes to the database schema and keep track of changes in your models.<\/p>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" id=\"h-creating-migrations\">Creating migrations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To create the migration file for your new model, run the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python manage.py makemigrations<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Django will analyze the changes in your <strong>models.py<\/strong> file and generate a migration file that describes those changes in a format that Django can apply to the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You\u2019ll see output similar to this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Migrations for 'myapp':\n\n\u00a0\u00a0myapp\/migrations\/0001_initial.py\n\n\u00a0\u00a0\u00a0\u00a0- Create model Book<\/pre>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" id=\"h-applying-migrations\">Applying migrations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After generating the migration, apply it to the database using the <strong>migrate<\/strong> command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python manage.py migrate<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command will create the necessary tables in your database. Now, the <strong>Book<\/strong> model is represented by a corresponding <strong>Book<\/strong> table in your database, and you can start adding, querying, and managing data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-setting-up-the-default-database\">Setting up the default database<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Django supports multiple databases, so you can choose the best one for your project. By default, Django uses <strong>SQLite<\/strong>, which is lightweight, easy to set up, and ideal for development and small projects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For production environments, we recommend more robust databases like <strong>PostgreSQL<\/strong>, <strong>MySQL<\/strong>, or <strong>MariaDB<\/strong> due to their scalability and performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, let\u2019s start by setting up the default SQLite database.<\/p>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" id=\"h-configuring-the-database-in-settings-py\">Configuring the database in settings.py<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By default, Django is configured to use SQLite, which comes pre-installed with Python, so no additional setup is needed. Here\u2019s how to make sure your project is set up to use SQLite:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to your project\u2019s <strong>settings.py<\/strong> file, which is located in the project folder (<strong>myproject\/myproject\/settings.py<\/strong>).<\/li>\n\n\n\n<li>Go to the <strong>DATABASES<\/strong> section in <strong>settings.py<\/strong>, where Django has already configured SQLite as the default database. It should look like this:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DATABASES = {\n\n\u00a0\u00a0\u00a0\u00a0'default': {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'ENGINE': 'django.db.backends.sqlite3',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'NAME': BASE_DIR \/ 'db.sqlite3',\n\n\u00a0\u00a0\u00a0\u00a0}\n\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ENGINE<\/strong> \u2013 specifies the database backend (in this case, SQLite).<\/li>\n\n\n\n<li><strong>NAME<\/strong> \u2013 the path to the SQLite database file (<strong>db.sqlite3<\/strong>). Django will automatically create this file when you run the initial migration.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" id=\"h-applying-migrations-for-default-tables\">Applying migrations for default tables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After confirming your database settings, you need to apply the initial migrations to set up the default tables (like <strong>users<\/strong> and <strong>sessions<\/strong>). Run the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python manage.py migrate<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command will create the <strong>db.sqlite3<\/strong> file in your project directory\u2019s root and set up all the necessary tables for your application to run.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the migrations are complete, check that <strong>db.sqlite3<\/strong> was created in your project folder. This file stores your database locally and will grow as you add more data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-using-other-databases-for-django-production\">Using other databases for Django production<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once you&#8217;ve set up the default SQLite database for development, you may need to integrate a more robust database management system (DBMS) as your project grows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Django supports multiple DBMSs, including <strong>PostgreSQL<\/strong>, <strong>MySQL<\/strong>, <strong>MariaDB<\/strong>, and <strong>Oracle<\/strong>. Each has its own strengths and requires a specific configuration for integration with Django.<\/p>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" id=\"h-overview-of-other-supported-databases-in-django\">Overview of other Supported databases in Django<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s take a look at each of the options and how they differ from SQLite:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>PostgreSQL<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A powerful, open-source database with advanced features, such as support for complex queries, data types like JSON, and full-text search. It&#8217;s highly recommended for production Django applications due to its scalability, robustness, and feature set.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL is known for strictly adhering to SQL standards and is reliable for complex and large-scale applications.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Setup<\/strong> \u2013 PostgreSQL requires installing the <strong>psycopg2<\/strong> package, which acts as the adapter between Django and PostgreSQL.<\/li>\n\n\n\n<li><strong>Configuration<\/strong> \u2013 in addition to the default <strong>ENGINE<\/strong> and <strong>NAME<\/strong> parameters for SQLite, PostgreSQL also requires <strong>USER<\/strong>, PASSWORD, <strong>HOST<\/strong>, and <strong>PORT<\/strong>. These parameters are configured in the <strong>settings.py<\/strong> file, under the <strong>DATABASES<\/strong> section:<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DATABASES = {\n\n\u00a0\u00a0\u00a0\u00a0'default': {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'ENGINE': 'django.db.backends.postgresql',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'NAME': 'mydatabase',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'USER': 'mydatabaseuser',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'PASSWORD': 'mypassword',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'HOST': 'localhost',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'PORT': '5432',\n\n\u00a0\u00a0\u00a0\u00a0}\n\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Differences<\/strong> \u2013 unlike SQLite, PostgreSQL is a fully-featured database that supports multiple concurrent users, advanced querying, and transactions. You&#8217;ll need to install PostgreSQL on your server, create a database and user, and configure more complex settings.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>MySQL and MariaDB<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Both are fast and reliable relational databases widely used in web development. MySQL is known for its speed, while MariaDB, a fork of MySQL, focuses on community-driven development and is fully compatible with MySQL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These databases are excellent for high-traffic web applications and are often favored for their performance.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Setup<\/strong> \u2013 MySQL and MariaDB both use the <strong>mysqlclient<\/strong> package to interface with Django.<\/li>\n\n\n\n<li><strong>Configuration<\/strong> \u2013 both use the same parameters as PostgreSQL, which are configured in the<strong> settings.py<\/strong> file under the <strong>DATABASES<\/strong> section:<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DATABASES = {\n\n\u00a0\u00a0\u00a0\u00a0'default': {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'ENGINE': 'django.db.backends.mysql',\u00a0 # or 'django.db.backends.mariadb' for MariaDB\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'NAME': 'mydatabase',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'USER': 'mydatabaseuser',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'PASSWORD': 'mypassword',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'HOST': 'localhost',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'PORT': '3306',\n\n\u00a0\u00a0\u00a0\u00a0}\n\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Differences<\/strong> \u2013 Compared to SQLite, MySQL and MariaDB are known for their performance with large datasets and high-traffic web applications. They support multiple storage engines, fast reads, and can handle millions of queries quickly.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Oracle and Microsoft SQL Server<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These enterprise-grade databases offer significant power, especially for large-scale web applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, their setup can be more complex, and they are typically used in specific enterprise environments where the business already relies on Oracle or SQL Server for other software.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Setup<\/strong> \u2013 both require additional drivers for integration with Django. For Oracle, you&#8217;ll need <strong>cx_Oracle<\/strong>, and for SQL Server, you&#8217;ll need <strong>pyodbc<\/strong>.<\/li>\n\n\n\n<li><strong>Configuration<\/strong> \u2013 each of these systems requires a more complex setup, including server installation, driver installation, and configuring database credentials in the <strong>settings.py<\/strong> file.<\/li>\n\n\n\n<li><strong>Differences<\/strong> \u2013 Oracle and SQL Server offer enterprise-grade features such as advanced transaction handling, optimized performance, and integration with large-scale systems. Their setup is more complex than SQLite and is typically handled by specialized database administrators.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" id=\"h-choosing-the-right-database-for-your-project\">Choosing the right database for your project<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">With so many different databases available, how do you choose the right one? Here\u2019s a quick, to-the-point breakdown:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Development and testing<\/strong> \u2013 stick with SQLite. It\u2019s simple, requires no setup, and is ideal for local testing.<\/li>\n\n\n\n<li><strong>Small-scale production<\/strong> \u2013 for smaller, low-traffic web applications, MySQL or MariaDB may be a better choice. They offer better performance than SQLite and are easier to learn than PostgreSQL.<\/li>\n\n\n\n<li><strong>Large-scale production<\/strong> \u2013 PostgreSQL is highly recommended for Django projects that require scalability, security, and advanced features.<\/li>\n\n\n\n<li><strong>Enterprise environments<\/strong> \u2013 if you work in an enterprise setting that already uses Oracle or SQL Server for other applications, these databases are likely the best choice due to integration requirements.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" id=\"h-setting-up-postgresql-with-django\">Setting up PostgreSQL with Django<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we\u2019ll walk through the process of integrating PostgreSQL with Django, as it\u2019s one of the most commonly used databases for production applications.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>To integrate PostgreSQL with Django, you first need to install it on your system:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt update\n\nsudo apt install postgresql postgresql-contrib<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>Django uses the <strong>psycopg2<\/strong> package to interact with PostgreSQL. Install it using <strong>pip<\/strong>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install psycopg2<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>Switch to the PostgreSQL user:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo -u postgres psql<\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>Create a new database:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE DATABASE mydatabase;<\/pre>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li>Create a user with a password:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE USER mydatabaseuser WITH PASSWORD 'mypassword';<\/pre>\n\n\n\n<ol start=\"6\" class=\"wp-block-list\">\n<li>Grant the user all privileges on the database:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">GRANT ALL PRIVILEGES ON DATABASE mydatabase TO mydatabaseuser;<\/pre>\n\n\n\n<ol start=\"7\" class=\"wp-block-list\">\n<li>Exit the PostgreSQL shell:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\\q<\/pre>\n\n\n\n<ol start=\"8\" class=\"wp-block-list\">\n<li>Open your <strong>settings.py file<\/strong> and locate the <strong>DATABASES<\/strong> section. Modify it for PostgreSQL as follows:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DATABASES = {\n\n\u00a0\u00a0\u00a0\u00a0'default': {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'ENGINE': 'django.db.backends.postgresql',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'NAME': 'mydatabase',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'USER': 'mydatabaseuser',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'PASSWORD': 'mypassword',\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'HOST': 'localhost',\u00a0 # Use '127.0.0.1' if 'localhost' doesn't work\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'PORT': '5432', \u00a0 \u00a0 \u00a0 # Default PostgreSQL port\n\n\u00a0\u00a0\u00a0\u00a0}\n\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The actual <strong>PASSWORD<\/strong> and <strong>USER<\/strong> values should match the ones you set in <strong>Step 5<\/strong>.<\/p>\n\n\n\n<ol start=\"9\" class=\"wp-block-list\">\n<li>Now that your database is configured, apply your project\u2019s migrations to set up the necessary tables in PostgreSQL:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python manage.py migrate<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command will create the required tables in your PostgreSQL database.<\/p>\n\n\n\n<ol start=\"10\" class=\"wp-block-list\">\n<li>Finally, run the Django development server to verify that the PostgreSQL integration is successful:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python manage.py runserver<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Open <strong>http:\/\/127.0.0.1:8000\/<\/strong> in your browser. If the Django welcome page appears without errors, your database connection is working correctly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In summary, Django makes working with databases simple and efficient. Its models let you define your database structure using Python, and migrations automatically handle database updates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For development, SQLite is ideal, but for larger, production-ready projects, PostgreSQL, MySQL, or MariaDB offer better scalability and performance. Don\u2019t worry; Django\u2019s flexibility ensures your app can scale seamlessly as it grows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, you can start working with <a data-click-id=\"hgr-post-content-link-django-views\" href=\"\/my\/tutorials\/building-django-views\/\">Django views<\/a> and <a data-click-id=\"hgr-post-content-link-templates\" href=\"\/my\/tutorials\/working-with-django-templates\/\">templates<\/a> to build dynamic content for your site.<\/p>\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-django-models-faq\">Django models FAQ<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-694483f8bb5f2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do Django models work?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Django models define the database structure using Python classes. Each model corresponds to a database table, and its attributes represent the table&#8217;s fields. When you run migrations, Django creates or updates the database tables based on your models.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-694483f8bb5f5\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do I create a Django model?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>To create a Django model, define a class in <strong>models.py<\/strong> that inherits from <strong>django.db.models.Model<\/strong> and specify the fields. Then, run <strong>python manage.py makemigrations<\/strong> and <strong>python manage.py migrate<\/strong> to update the database.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-694483f8bb5f6\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can I use multiple databases with Django models?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, Django supports multiple databases. You can configure them in<strong> settings.py<\/strong> and use the <strong>using()<\/strong> method to specify which database a query should use.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Django models are the heart of data management in any Django application. They define the structure of your database and serve as the foundation for creating dynamic, data-driven applications. With Django\u2019s powerful object-relational mapping (ORM) system, working with models becomes seamless, enabling you to define, query, and manipulate data effortlessly. Let&#8217;s explore how Django models [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/my\/tutorials\/django-models-and-databases\/\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":185,"featured_media":120144,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to work with Django models and databases","rank_math_description":"Learn how Django models work, how to create and migrate them, and explore database options like PostgreSQL for production environments.","rank_math_focus_keyword":"","_content_updated_date":"","footnotes":""},"categories":[],"tags":[],"class_list":["post-120137","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry"],"hreflangs":[],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/120137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/users\/185"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/comments?post=120137"}],"version-history":[{"count":6,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/120137\/revisions"}],"predecessor-version":[{"id":126021,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/120137\/revisions\/126021"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/media\/120144"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/media?parent=120137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/categories?post=120137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/tags?post=120137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}