Python Django - The Practical Guide
Lecture 110: Migrations & Admin Login-opendir.cloud
Introduction
In this lecture, we'll be discussing migrations and admin login in Python Django. Migrations are a way to keep track of changes to your database schema, and admin login allows you to access a web-based interface for managing your Django data.
Migrations
Migrations are a new feature in Django 1.7. They allow you to track changes to your database schema over time. This is useful for a few reasons:
It makes it easy to deploy your Django application to a new server, or to update the schema of an existing deployment.
It makes it easy to collaborate with other developers on a Django project.
It allows you to rollback changes to your database schema if necessary.
To create a migration, you can use the `makemigrations` command. This will create a new Python file in the `migrations` directory of your Django project. The file will contain a migration class, which defines the changes to the database schema that will be made by the migration.
Once you've created a migration, you can run it using the `migrate` command. This will update your database schema to match the changes defined in the migration class.
Admin Login
Django includes a built-in admin interface that allows you to manage your Django data through a web-based interface. To access the admin interface, you'll need to create a superuser account. You can do this using the `createsuperuser` command.
Once you've created a superuser account, you can log in to the admin interface by visiting the `/admin/` URL in your web browser. You'll be prompted to enter your superuser username and password.
After logging in, you'll see a list of all the Django models in your project. You can click on any model to view a list of all the objects of that type. You can also create, edit, and delete objects from the admin interface.
Example
Here's a simple example of how to use migrations and admin login in Django:
```python
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = []
operations = [
migrations.CreateModel(
name='BlogPost',
fields=[
('id', models.AutoField(primary_key=True)),
('title', models.CharField(max_length=255)),
('content', models.TextField()),
('created_at', models.DateTimeField(auto_now_add=True)),
],
),
]
```
To create a migration for this model, we can use the following command:
```
python manage.py makemigrations
```
This will create a new Python file in the `migrations` directory of our Django project. The file will contain a migration class, which defines the changes to the database schema that will be made by the migration.
To run the migration, we can use the following command:
```
python manage.py migrate
```
This will update our database schema to match the changes defined in the migration class.
Now that we have a migration, we can create a superuser account and log in to the admin interface. Once we're logged in, we'll see the `BlogPost` model in the list of Django models. We can click on the model to view a list of all the blog posts in our database. We can also create, edit, and delete blog posts from the admin interface.
Conclusion
Migrations and admin login are two powerful features of Django that can make it easier to develop and deploy Django applications. By using migrations, you can track changes to your database schema over time, and by using admin login, you can access a web-based interface for managing your Django data.
*Please do follow us for more Python Django tutorials.*