Curso Django. Panel de Administración II. Vídeo 17
Introduction to Django Admin Panel
In this video, the speaker introduces the Django admin panel and explains how to manipulate tables in the database using the admin panel. The focus is on importing models and registering them in the admin panel.
Importing Models and Registering Tables
- In the previous videos, it was mentioned that modifications need to be made in the
admin.pyfile to manipulate tables.
- To modify a specific table (e.g., "clientes"), import the corresponding model by adding
from gestion_de_pedidos.models import clientesafter the existing import statement.
- Include the following line of code to make the "clientes" table available in the admin panel:
admin.site.register(clientes)
- After saving changes, start the server (
python manage.py runserver) and access the admin panel URL.
- The admin panel now displays a table for "clientes" with buttons for adding and changing entries.
Manipulating Table Entries
- Clicking on "change" allows modifying existing entries, while clicking on "add" enables creating new entries.
- Deleting an entry is possible by clicking on "delete".
- Changes made through the admin panel are reflected in the database. Refreshing or updating may be required to see these changes.
- Adding a new user requires filling in the required fields, which are displayed in bold.
- After saving a new entry, it is confirmed that the entry has been successfully saved by checking the database.
- To add other tables (e.g., "artículos" and "pedidos"), import their corresponding models and register them in the admin panel using
admin.site.register().
- After updating the admin panel, the newly added tables ("artículos" and "pedidos") will be visible.
Understanding Django Admin Interface
In this section, the speaker explains the difference in information display between different tables in the Django admin interface. The use of the __str__ method in the model is highlighted as a way to control how object information is displayed.
Displaying Object Information
- The speaker demonstrates that in the "Artículos" table, each item's information is displayed with a specific format, while in the "Clientes" table, it appears differently.
- This difference is due to the implementation of the
__str__method in the model for "Artículos," which converts object information into a readable string format.
- By implementing a similar
__str__method for "Clientes," we can achieve consistent and readable display of object information.
Modifying Object Information
- Clicking on any record in the "Artículos" table allows modification of its information.
- Fields like price can be modified using arrows or directly entering new values.
- After saving changes, updating the corresponding database entry reflects these modifications.
Making Fields Optional
- To make a field optional instead of required, such as making email optional for clients:
- Modify the corresponding field in the model class by adding
blank=Trueandnull=True.
- Save changes and perform migrations using
python manage.py makemigrationsfollowed bypython manage.py migrate.
Testing Optional Field
- After making email optional for clients, creating a new client entry without an email shows that it accepts empty values.
- Checking the database confirms that null values are allowed for this field.
Migrating Changes to Models
In this section, steps for migrating changes made to models are explained. The importance of running migrations after modifying models is emphasized.
Running Migrations
- After making changes to models, migrations need to be run for the changes to take effect.
- Use the command
python manage.py makemigrationsfollowed bypython manage.py migrateto perform migrations.
Verifying Changes
- After running migrations, check if the changes have been successfully applied by examining the output of the migration command.
- The modified field should be detected and displayed in the migration output.
Updating Django Admin Interface
This section demonstrates how modifications made to models affect the Django admin interface. The speaker shows that making email optional for clients removes its bold formatting in the admin interface.
Checking Updated Interface
- After making email optional for clients, accessing the admin interface shows that the email field is no longer displayed in bold.
- Creating a new client entry without an email confirms that it is now considered optional.
Conclusion and Additional Resources
The speaker concludes by offering additional resources and inviting viewers to register for free courses on their virtual academy website.
Additional Resources
- Viewers can register for free courses on Píldoras Informáticas' virtual academy website.
- Free courses are organized by categories and cover various topics related to programming and technology.
Turn any video into a summary like this
YouTube links, meetings, lectures. With transcripts, search, and chat.