Chapter 3. Django Models and Database

Add Models in Django Admin – admin.py

Add Models in Django Admin – admin.py
Tag:

Even though you have migrated Django models into a database, you cannot see it instantly. The quickest way to see the database is through the Django admin site. To see your database in the Django admin site, you need to add some code in the admin.py file under the app directory.

To check the status, run the runserver command.

Command Line - INPUT
python manage.py runserver

Go to localhost:8000/admin/ in your browser. You can confirm that there is no model besides the models already prepared (i.e., Groups and Users).

Add-Models-in-Django-Admin--adminpy

Edit admin.py

Adding new models to the admin.py file is straightforward.

You need to import a model you want to add and register the model. You can register the two models we created in the previous sections by adding the yellow line below.

employee_learning/admin.py
from django.contrib import admin

# Register your models here.
from .models import Employee
from .models import Division

admin.site.register(Employee)
admin.site.register(Division)

Go to localhost:8000/admin/ again. You'll see that the two models are added to the admin site. You can manage the process to update the admin.py file without stopping and re-running the runserver command.

Add-Models-in-Django-Admin--adminpy

Even though you have migrated Django models into a database, you cannot see it instantly. The quickest way to see the database is through the Django admin site. To see your database in the Django admin site, you need to add some code in the admin.py file under the app directory.

To check the status, run the runserver command.

Command Line - INPUT
python manage.py runserver

Go to localhost:8000/admin/ in your browser. You can confirm that there is no model besides the models already prepared (i.e., Groups and Users).

Add-Models-in-Django-Admin--adminpy

Edit admin.py

Adding new models to the admin.py file is straightforward.

You need to import a model you want to add and register the model. You can register the two models we created in the previous sections by adding the yellow line below.

employee_learning/admin.py
from django.contrib import admin

# Register your models here.
from .models import Employee
from .models import Division

admin.site.register(Employee)
admin.site.register(Division)

Go to localhost:8000/admin/ again. You'll see that the two models are added to the admin site. You can manage the process to update the admin.py file without stopping and re-running the runserver command.

Add-Models-in-Django-Admin--adminpy

Tag: