How to register a model in django

How to register a model in django

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

Create your project folder e.g "my project" and open it in your code editor

Now let's create a virtual environment for our project

python -m venv venv

let's activate our virtual environment for windows

venv\scripts\activate

for macs

venv/bin/activate

Let's talk about Apps and Project

An app is a Web application that does something. e.g e-commerce sites

A project is a collection of configuration and apps for a particular website

Let's install django and then start our project and app

pip install django
django-admin startproject blog .

start your app

python manage.py startapp account

register your app in sittings.py file. Our sittings.py file is in our blog folder, so open and look for INSTALLED_APPS and add this code

'account.apps.AccountConfig',

Remember that our app name is account. So, we registered it in our sittinga.py file.

Let's create our model name Profile. Open your models.py file in your account folder and add this code

class Profile(models.Model):
    name = models.CharField(max_length = 300)
    date_entrolled = models.DateField()
    bio = models.TextField()

    def __str__(self):
        return self.name

Now let's register our model in our admin. Open your admin.py file and add this code

from .models import Profile

admin.site.register(Profile)

Now, we've successfully registered our model. let's make our migration to sync our models

python manage.py makemigrations

Then we migrate

python manage.py migrate

Let's create admin account for our project

python manage.py createsuperuser

After that you can now run your server

python manage.py runserver

Open your browser to this address http://127.0.0.1:8000/

Login to the admin area 127.0.0.1:8000/admin

after successful login you'll see this page

Capture.PNG

On the profiles, you can click on add. Then after adding the required field you can then save.

that's all for registering model for beginners