Hướng dẫn học django phần i models api và admin

Now that we've created models for the LocalLibrary website, we'll use the Django Admin site to add some "real" book data. First we'll show you how to register the models with the admin site, then we'll show you how to login and create some data. At the end of the article we will show some of the ways you can further improve the presentation of the Admin site.

Show

    Prerequisites: First complete: Django Tutorial Part 3: Using models. Objective: To understand the benefits and limitations of the Django admin site, and use it to create some records for our models.

    The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data. The admin application can also be useful for managing data in production, depending on the type of website. The Django project recommends it only for internal data management (i.e. just for use by admins, or people internal to your organization), as the model-centric approach is not necessarily the best possible interface for all users, and exposes a lot of unnecessary detail about the models.

    All the configuration required to include the admin application in your website was done automatically when you created the skeleton project (for information about actual dependencies needed, see the Django docs here). As a result, all you must do to add your models to the admin application is to register them. At the end of this article we'll provide a brief demonstration of how you might further configure the admin area to better display our model data.

    After registering the models we'll show how to create a new "superuser", login to the site, and create some books, authors, book instances, and genres. These will be useful for testing the views and templates we'll start creating in the next tutorial.

    First, open admin.py in the catalog application (/locallibrary/catalog/admin.py). It currently looks like this — note that it already imports

    from .models import Author, Genre, Book, BookInstance, Language
    admin.site.register(Book)
    admin.site.register(Author)
    admin.site.register(Genre)
    admin.site.register(BookInstance)
    admin.site.register(Language)
    

    5:

    from django.contrib import admin
    # Register your models here.
    

    Register the models by copying the following text into the bottom of the file. This code imports the models and then calls

    from .models import Author, Genre, Book, BookInstance, Language
    admin.site.register(Book)
    admin.site.register(Author)
    admin.site.register(Genre)
    admin.site.register(BookInstance)
    admin.site.register(Language)
    

    6 to register each of them.

    from .models import Author, Genre, Book, BookInstance, Language
    admin.site.register(Book)
    admin.site.register(Author)
    admin.site.register(Genre)
    admin.site.register(BookInstance)
    admin.site.register(Language)
    

    Note: The lines above assume that you accepted the challenge to create a model to represent the natural language of a book (see the models tutorial article)!

    This is the simplest way of registering a model, or models, with the site. The admin site is highly customizable, and we'll talk more about the other ways of registering your models further down.

    In order to log into the admin site, we need a user account with Staff status enabled. In order to view and create records we also need this user to have permissions to manage all our objects. You can create a "superuser" account that has full access to the site and all needed permissions using manage.py.

    Call the following command, in the same directory as manage.py, to create the superuser. You will be prompted to enter a username, email address, and strong password.

    python3 manage.py createsuperuser
    

    Once this command completes a new superuser will have been added to the database. Now restart the development server so we can test the login:

    python3 manage.py runserver
    

    To login to the site, open the /admin URL (e.g.

    from .models import Author, Genre, Book, BookInstance, Language
    admin.site.register(Book)
    admin.site.register(Author)
    admin.site.register(Genre)
    admin.site.register(BookInstance)
    admin.site.register(Language)
    

    1. and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

    This part of the site displays all our models, grouped by installed application. You can click on a model name to go to a screen that lists all its associated records, and you can further click on those records to edit them. You can also directly click the Add link next to each model to start creating a record of that type.

    Hướng dẫn học django phần i models api và admin

    Click on the Add link to the right of Books to create a new book (this will display a dialog much like the one below). Note how the titles of each field, the type of widget used, and the

    from .models import Author, Genre, Book, BookInstance, Language
    admin.site.register(Book)
    admin.site.register(Author)
    admin.site.register(Genre)
    admin.site.register(BookInstance)
    admin.site.register(Language)
    

    8 (if any) match the values you specified in the model.

    Enter values for the fields. You can create new authors or genres by pressing the + button next to the respective fields (or select existing values from the lists if you've already created them). When you're done you can press SAVE, Save and add another, or Save and continue editing to save the record.

    Hướng dẫn học django phần i models api và admin

    Note: At this point we'd like you to spend some time adding a few books, authors, languages, and genres (e.g. Fantasy) to your application. Make sure that each author and genre includes a couple of different books (this will make your list and detail views more interesting when we implement them later on in the article series).

    When you've finished adding books, click on the Home link in the top bookmark to be taken back to the main admin page. Then click on the Books link to display the current list of books (or on one of the other links to see other model lists). Now that you've added a few books, the list might look similar to the screenshot below. The title of each book is displayed; this is the value returned in the Book model's

    from .models import Author, Genre, Book, BookInstance, Language
    admin.site.register(Book)
    admin.site.register(Author)
    admin.site.register(Genre)
    admin.site.register(BookInstance)
    admin.site.register(Language)
    

    9 method that we specified in the last article.

    Hướng dẫn học django phần i models api và admin

    From this list you can delete books by selecting the checkbox next to the book you don't want, selecting the delete… action from the Action drop-down list, and then pressing the Go button. You can also add new books by pressing the ADD BOOK button.

    You can edit a book by selecting its name in the link. The edit page for a book, shown below, is almost identical to the "Add" page. The main differences are the page title (Change book) and the addition of Delete, HISTORY and VIEW ON SITE buttons (this last button appears because we defined the

    python3 manage.py createsuperuser
    

    0 method in our model).

    Note: Clicking the VIEW ON SITE button raises a

    python3 manage.py createsuperuser
    

    1 exception because the

    python3 manage.py createsuperuser
    

    0 method attempts to

    python3 manage.py createsuperuser
    

    3 a named URL mapping ('book-detail') that has not yet been defined. We'll define a URL mapping and associated view in Django Tutorial Part 6: Generic list and detail views.

    Hướng dẫn học django phần i models api và admin

    Now navigate back to the Home page (using the Home link in the breadcrumb trail) and then view the Author and Genre lists — you should already have quite a few created from when you added the new books, but feel free to add some more.

    What you won't have is any Book Instances, because these are not created from Books (although you can create a

    python3 manage.py createsuperuser
    

    4 from a

    python3 manage.py createsuperuser
    

    5 — this is the nature of the

    python3 manage.py createsuperuser
    

    6 field). Navigate back to the Home page and press the associated Add button to display the Add book instance screen below. Note the large, globally unique Id, which can be used to separately identify a single copy of a book in the library.

    Hướng dẫn học django phần i models api và admin

    Create a number of these records for each of your books. Set the status as Available for at least some records and On loan for others. If the status is not Available, then also set a future Due back date.

    That's it! You've now learned how to set up the administration site in both its simplest and improved form, how to create a superuser, and how to navigate the admin site and view, delete, and update records. Along the way you've created a bunch of Books, BookInstances, Genres, and Authors that we'll be able to list and display once we create our own view and templates.