Create secrets application
- 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:
- Models: Define your database structure using Django models in your app’s
models.py
file. Each model represents a database table. - Settings Configuration: Ensure your Django project’s
settings.py
file is properly configured, including the database settings such asDATABASES
to specify the database connection. - Installed Apps: Make sure your app is listed in the
INSTALLED_APPS
setting insettings.py
. This tells Django which apps are included in the project. - 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:
- Make Migrations: Run
python manage.py makemigrations
to create migration files based on the changes you've made to your models. - 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.