Skip to the content.

What we will learn

The source of this summary The first link

The source of this summary The second link


Django Models

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

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.

#### Common field types

Creating and modifying records

save() :To create a record you can define an instance of the model

Searching for records

  1. We can get all records for a model as a QuerySet, using objects.all(). The QuerySet is an iterable object, meaning that it contains a number of objects that we can iterate/loop through.
  2. filter() method allows us to filter the returned QuerySet to match a specified text or numeric field against particular criteria.

The Django admin site

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 , 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.

Registering models

This code imports the models and then calls admin.site.register to register each of them.

Creating a superuser

  1. For creating superuser, first reach the same directory as that of manage.py
  2. enter the Username of your choice and press enter.

    Username:

  3. enter the Email address and press enter.(It can be left blank)

    Email address:

  4. enter the Password in-front of the Password field and press enter.Enter a strong password so as to keep it secure.

    Password: ***

Logging in and using the site