How to create your first project Python Django | Models | Episode 03
HTML-код
- Опубликовано: 27 ноя 2024
- How to create your first project Python Django | Models | Episode 03
Creating models in a Django project is a fundamental step in defining the structure of your database. Models represent the data and define how it is stored, queried, and manipulated. To create your first Django model, follow these steps:
*Step 1: Create a Django Project and App*
Before you can create models, you need to set up a Django project and create an app. If you haven't done this already, follow these steps:
1. Create a new Django project:
```bash
django-admin startproject myproject
```
2. Change your working directory to the project folder:
```bash
cd myproject
```
3. Create a new Django app (replace `myapp` with your desired app name):
```bash
python manage.py startapp myapp
```
*Step 2: Define Your Model*
In your app's directory (e.g., `myapp`), open the `models.py` file or create it if it doesn't exist. This is where you define your models using Python classes.
For example, let's create a simple model to represent movies:
```python
from django.db import models
class Movie(models.Model):
title = models.CharField(max_length=100)
release_date = models.DateField()
genre = models.CharField(max_length=50)
def __str__(self):
return self.title
```
In this example, we define a `Movie` model with three fields: `title`, `release_date`, and `genre`. The `__str__` method is used to provide a human-readable representation of the model.
*Step 3: Create Migrations*
Django models are translated into database tables. To create the corresponding database schema, you need to generate migrations based on your models. Run the following command:
```bash
python manage.py makemigrations
```
Django will analyze your models and create migration files in the `myapp/migrations` directory.
*Step 4: Apply Migrations*
Apply the migrations to update your database schema:
```bash
python manage.py migrate
```
This command will create the actual database tables based on your model definition.
*Step 5: Use the Model*
You can now use your model to interact with the database. You can create, retrieve, update, and delete instances of the `Movie` model. For example, you can add new movies to the database:
```python
In a Python script or Django shell
from myapp.models import Movie
Create a new movie
new_movie = Movie(title="Inception", release_date="2010-07-16", genre="Science Fiction")
new_movie.save()
```
*Step 6: Admin Interface (Optional)*
To make it easier to manage your models, you can set up the Django admin interface. First, create a superuser account if you haven't already:
```bash
python manage.py createsuperuser
```
Then, add your model to the admin interface. In your app's `admin.py` file (create it if it doesn't exist), register the model like this:
```python
from django.contrib import admin
from .models import Movie
admin.site.register(Movie)
```
Now, you can access the admin interface at `127.0.0.1:8000/admin/` and manage your `Movie` model.
You've created your first Django model. You can continue building your application by defining more models, views, and templates to create a fully functional web application.