Reading-Notes


Project maintained by eslamakram Hosted on GitHub Pages — Theme by mattgraham

Django Using models

what a model is, how it is declared, and some of the main field types. It also briefly shows a few of the main ways you can access model data??!! Django web applications access and manage data through Python objects referred to as models. Models define the structure of stored data, including the field types and possibly also their maximum size, default values, selection list options, help text for documentation, label text for forms, etc. The definition of the model is independent of the underlying database — you can choose one of several as part of your project settings. Once you’ve chosen what database you want to use, you don’t need to talk to it directly at all — you just write your model structure and other code, and Django handles all the dirty work of communicating with the database for you.

Model definition Models are usually defined in an application’s models.py file. They are implemented as subclasses of django.db.models.Model, and can include fields, methods and metadata.

Fields A model can have an arbitrary number of fields, of any type — each one represents a column of data that we want to store in one of our database tables. Each database record (row) will consist of one of each field value. Let’s look at the example seen below:

 my_field_name = models.CharField(max_length=20, help_text='Enter field documentation')

Common field types: The following list describes some of the more commonly used types of fields.

Django admin site: 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.

  1. Creating a superuser 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

  1. Logging in and using the site To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin) 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).

  2. Advanced configuration Django does a pretty good job of creating a basic admin site using the information from the registered models:

Each model has a list of individual records, identified by the string created with the model’s str() method, and linked to detail views/forms for editing. By default, this view has an action menu at the top that you can use to perform bulk delete operations on records. The model detail record forms for editing and adding records contain all the fields in the model, laid out vertically in their declaration order.