Learn Django from scratch with a small project _3
Published: 2020-07-23 20:37:09

Welcome back. Today we will be discussing how to use the admin feature of Django and how to create Django models.

Launch your server. From the last article, you shd have this page up and running. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pk4q3lo7kcin2dt0f5fg.png)

To the end of the url, add "/admin/". This page should come up:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vedhndsjovyasx8pkr7y.png)

Now we need to create our admin credentials so that we can log in. Basically we need to create a new superuser.

```python
python manage.py createsuperuser
```

Fill in the details, and create your new superuser.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0wpnzjk09urhx0n43cfi.png)

Go back to the login page and fill in the credentials you just created.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hc3x9o3hnelkeghrt8j8.png)

Click on users and see the details you just created

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kifv6o4qqtc7qgx4m6m7.png)


CREATE A MODEL
The idea of DJANGO Models is that instead of writing everything afresh we can write models which will automatically be transformed into tables by Django

Go to "models.py" and let's create a new model class which we will call 'User'. Note that the first letter is capitalized to indicate that its a class.

```python
from django.db import models

# Create your models here.
class User(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField()
    comment = models.TextField()

```

You can go to the official documentation to learn more about model fields

Next

```python
python manage.py migrate
```
then

```python
python manage.py makemigrations
```

You shd notice a new file created(0001_initial.py) within your migrations folder.

Then run again:

```python
python manage.py migrate
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vii265ohkzzxm9d78nlc.png)

```python

from django.db import migrations, models

class Migration(migrations.Migration):
    initial = True
    dependencies = [
    ]
    operations = [
        migrations.CreateModel(
            name='User',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('first_name', models.CharField(max_length=100)),
                ('last_name', models.CharField(max_length=100)),
                ('email', models.EmailField(max_length=254)),
                ('comment', models.TextField()),
            ],
        ),
    ]
```
Notice all the new fields we created.

Lastly we need to alert django admin to the presence of a new model. Within "admin.py" add:

```python
from django.contrib import admin
from date_app.models import User

# Register your models here.
admin.site.register(User)
```
Run the server again, and within it, you shd have a new "User" model within the date_app

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/06qlfpz90qjo5lbiq9z2.png)

Click on "add" and notice the four fields we created within the models.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/azvjyhl16d0v0zlpunk1.png)


Now try creating different user objects and saving them

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/engm58kd1be415tq0dbd.png)

Congratulations, we have just learnt how to use the Django admin and how to create models.