Software Dev: Django Setup

Iverique
2 min readApr 13, 2024

--

Create secrets application

  1. Using PyCharm, File> New Project

Result:

2. Add Models.py to your application

from django.db import models
from django.contrib.auth.models import User, Group


class Secret(models.Model):
created_at = models.DateTimeField(auto_now=True)
creator = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=255)
value = models.TextField()


class GroupSecret(models.Model):
created_at = models.DateTimeField(auto_now=True)
creator = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=255)
value = models.TextField()
group = models.ForeignKey(Group, on_delete=models.CASCADE, related_name="group_secrets")

3. Add migrations, migrate and update database

Tools > Run manage.py Task…

To make migrations in Django, you need the following:

  1. Models: Define your database structure using Django models in your app’s models.py file. Each model represents a database table.
  2. Settings Configuration: Ensure your Django project’s settings.py file is properly configured, including the database settings such as DATABASES to specify the database connection.
  3. Installed Apps: Make sure your app is listed in the INSTALLED_APPS setting in settings.py. This tells Django which apps are included in the project.
  4. Migrations Folder: Django maintains a folder named migrations within each app directory to track changes to the database schema. Make sure this folder exists and is populated with migration files if you've made changes to your models.

Once these prerequisites are in place, you can run the following commands to make and apply migrations:

  1. Make Migrations: Run python manage.py makemigrations to create migration files based on the changes you've made to your models.
  2. Apply Migrations: After making migrations, run python manage.py migrate to apply those changes to your database. This command synchronizes the database schema with the current state of your models.

--

--

No responses yet