Django (iii) the first Django app to create a model

Source: Internet
Author: User
Tags what sql

After the last chapter, we've created the good one Django app, and then we're ready to refine it.

First, let's start with some basic concepts.

Each application created in Django is made up of Python packages that follow certain conventions. Django comes with a utility that automatically generates the basic directory structure of an application, so you can focus on writing code rather than creating a directory.

So what's the difference between a project and an application?

An app is a web app: Like a blogging system, a simple voting system. and A project is a collection of configurations and applications specific Web site. A project can contain multiple applications. The application can exist in more than one project.

The application can be placed under any Python path. In this article, we put the app in the same directory as the manage.py file, so that it can act as a top-level module, not as a MySite sub-module.

So first, we go into the manage.py's sibling directory, which is C:\mysite, enter the following command:

manage.py Startapp Polls

The directory structure of the polls is as follows:

polls/    __init__.py    models.py    tests.py    views.py

The first step in writing a database Web application is to define the model, essentially using some extra metadata to define the database layout. patterns require defining the necessary fields and data behavior.

In our program, we need to build two models: Poll and Choice,poll contain the date of the question and the question, Choice contains the voting options and the count of votes, each Choice associated with a Poll.

modify polls/models.py below:

# -*-coding:utf-8-*-  from Import Models class Poll (models. Model):    = models. Charfield (max_length=200)    = models. Datetimefield ('date published')class  Choice (models. Model):    = models. ForeignKey (Poll)    = models. Charfield (max_length=200)    = models. Integerfield (default=0)

The code here is intuitive, each model represents a subclass of Django.db.models.Model, and each model has a number of variables that define the type and scope of the data.

Each variable is an instance of a field, Charfield represents a String field, Datetimefield represents a Date field, and each field instance (such as question or pub_date) represents a column in the database. you can enter a string in the first parameter position field to tell people what the variable means, such as ' Date published '. Some variables you need to set up some initial parameters for him. Like Integerfield. There will be some foreign keys between the table and the table, which can be set with ForeignKey. The foreign keys in Django are Many-to-ones, Many-to-manys and one-to-ones.

This code will tell Django to do the following things:

  • Create a data structure for this application (create claims for these tables).
  • Create data interfaces for objects such as poll and choice.

Before we do this, we first have to tell the program that the poll application has been built. Modify the Installed_apps in setting.py and add polls to it as follows:

Installed_apps = (    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.sites',    'django.contrib.messages',    'Django.contrib.staticfiles',    'polls'    #uncomment the next line to enable the admin:    #' Django.contrib.admin ',    #uncomment the next line to enable admin documentation:    #' Django.contrib.admindocs ',)

Now that the program knows that the polls application is well, here is another command to execute:

manage.py SQL Polls

The following appears:

BEGIN; CREATE TABLE"Polls_poll" (    "ID"integer not NULL PRIMARY KEY,"question"VARCHAR (200) not NULL,"pub_date"datetime not NULL); CREATE TABLE"Polls_choice" (    "ID"integer not NULL PRIMARY KEY,"poll_id"Integer not NULL REFERENCES"Polls_poll"("ID"),    "Choice_text"VARCHAR (200) not NULL,"votes"integer not NULL); COMMIT;

Let's explain it:

The Polls_poll and Polls_choice are automatically generated based on the variables in the model, and because we don't have a primary key set, the system sets the primary key to ID.

The SQL command does not really go to the database to execute the SQL statement-it just prints to the screen, so you can see what SQL Django thinks is necessary.

Here are a few commands:

manage.py Validate

Used to check that the model is correct.

manage.py Sqlcustom Polls

Output any custom SQL statements (such as modifications to the table or constraints defined for the application).

manage.py Sqlclear Polls

Output the necessary drop TABLE statements for this application, depending on which tables already exist in your database (if any).

manage.py sqlindexes Polls

Output the CREATE INDEX statement for this application.

manage.py Sqlall Polls

A collection of all the above commands.


Now, you can create tables for these models and execute:

manage.py syncdb

Note that this command will only create a new table for a table app that has not been created.

Django (iii) the first Django app to create a model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.