Create models
Every app written by Django contains a Python package that follows a specific convention. Django includes a tool that automatically generates a basic directory structure, all without having to create a directory, just focus on writing code.
Database of public records or a simple poll app. A project is a collection of the configuration and apps for a particular Web site. A Project
can contain multiple apps. An app can is in multiple projects.
Note: Apps is a Web application that can be a blog system, a simple voting system. A project is a collection of configurations and applications (apps). Project can contain multiple apps. An apps can be pure in multiple project.
To create an apps, make sure the current directory and manage.py are in the same directory, and type the following directives:
$ python manage.py Startapp polls
polls/ __init__.py admin.py migrations/ __init__.py models.py tests.py views.py
The first step in writing a database Web app is to define your models (essentially, the metadata design of the database).
Essentially just a history this Django can roll through to update your the database schema to match your current models.
In our simple poll app, we will create two models:Question and Choice. Question has a question and publication date. Choice has two fields: Choice text and ballot statistics. Each of the choice is associated with a question.
These concepts are represented by a simple Python class. Edit the polls/models.py file as follows:
from Import Models class Question (models. Model): = models. Charfield (max_length=200) = models. Datetimefield ('date published')class Choice (models. Model): = models. ForeignKey (Question) = models. Charfield (max_length=200) = models. Integerfield (default=0)
The code above is very intuitive. Each Model is represented by a Django.db.models.Model subclass. Each model has some class variables, each representing the fields of the database.
Each field was represented by an instance of aFieldClass–e.g.,CharfieldFor character fields andDatetimefieldFor DateTimes. This tells
Django what type of data each field holds. The name of eachFieldinstance (e.g.Question_textOrpub_date) is the field ' s name, in machine-friendly format. You'll use the this value in your
Python code, and your database would use it as the column name. You can use an optional first positional argument to aFieldTo designate a human-readable name. That's used in a couple of introspective parts of
Django, and it doubles as documentation. If This field isn ' t provided, Django would use the machine-readable name. In this example, we ' ve only defined a
Human-readable name forquestion.pub_date. For all and the field ' s machine-readable name would suffice as its human-readable name. SomeFieldClasses has required arguments.Charfield, for example, requires so give it aMax_length. That's used not only in the
Database schema, but in validation, as we'll soon see. AFieldcan also have various optional arguments; In this case, we ' ve set thedefaultValue ofvotesTo 0.Finally, note a relationship is defined, usingForeignKey. That's tells Django eachChoiceis related to a singleQuestion. Django supports all the
Common database Relationships:many-to-one, Many-to-many and one-to-one.
Activating models
Such a small piece of code provides a lot of information for Django. With it, Django can do these things:
- Create a database structure (schema, create TABLE statements)
- Create a Python database API that accesses Question and Choice objects
But the first thing to do is tell our project that the polls app will be installed.
Philosophy Django apps is "pluggable": You can use a app in multiple projects, and you can distribute apps, because they don ' t has To is tied to a given Django installation.
Edit the file mysite/settings.py and modify the Installed_apps settings, including the string ' polls '. As shown below:
Installed_apps = ( 'Django.contrib.admin', 'Django.contrib.auth', 'Django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'Django.contrib.staticfiles', 'polls',)
Django Web Development Introductory Tutorial (II)