
Good day!
This topic is the first part of the translation of the
Django documentation , namely the section on
models . Just want to say that I am not a professional translator and initially did the translation for myself. All comments and questions please write in the comments or send by habrapochta. I hope the information will be useful to you.
')
_Models
___ A small example
___Use of models
___ Fields
_____ Field Types
_____ Field Options
_____ Automatic Primary Keys
_____ Detailed field namesTranslation Django Documentation: Models. Part 2Translation Django Documentation: Models. Part 3Translation Django Documentation: Models. Part 4 (Last)This document refers to the Django's SVN release, which may differ significantly from previous ones. Old documentation (for Django 1.0) is available here.Models
The model is the only and definitive source of information about your data. It describes the behavior of the data you are storing and contains their main fields. As a rule, each model is a representation of one of the database tables.
Basic information:
- Each model is a class that is a subclass of django.db.models.Model .
- Each attribute of the model represents a separate database field.
- Using models, Django actually provides you with an automatically-generated database access API (see creating queries ).

An addendum to this document is the
official repository of examples of using models (You can also find these examples in the Django distribution in the
tests / modeltests directory ).
Small exampleIn this example, the
Person class is defined, containing the fields
first_name and
last_name :
Copy Source | Copy HTML<br/> from django.db import models<br/> <br/> class Person (models.Model):<br/> first_name = models.CharField(max_length= 30 )<br/> last_name = models.CharField(max_length= 30 ) <br/>
First_name and
last_name are the fields of our model. Each field defines a class attribute, and each attribute in turn is a database column.
The above
Person class will create a table with a database of the following form:
Copy Source | Copy HTML<br/> CREATE TABLE myapp_person (<br/> "id" serial NOT NULL PRIMARY KEY ,<br/> "first_name" varchar( 30 ) NOT NULL ,<br/> "last_name" varchar( 30 ) NOT NULL <br/>); <br/>
Some technical notes:
- The name of the table, myapp_person , is automatically obtained from the metadata of our model, however, it can be overridden (see the names of the tables ).
- The id field is added automatically, but you can change this behavior.
- In our example, SQL syntax is used that corresponds to PostgreSQL, however, it is worth noting that Django uses SQL based on the database specified in your settings file .
Use of modelsOnce you have identified your models, you must tell Django that you are going to use them. This is done by changing your configuration file, namely editing
INSTALLED_APPS , to which you need to add the name of your module containing
models.py .
For example, if the models of your application are in the module
mysite.myapp.models (this structure is created after the execution of the
manage.py startapp script ), the part of
INSTALLED_APPS we need should look like this:
Copy Source | Copy HTML<br/>INSTALLED_APPS = (<br/> #... <br/> 'mysite.myapp' ,<br/> #... <br/>) <br/>
After editing
INSTALLED_APPS, remember to run the
syncdb script.
FieldsMost Important and http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.__unicode__
A necessary part of any model is the list of database fields that it defines. Fields are set by class attributes.
For example: http: //docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.__unicode__
Copy Source | Copy HTML<br/> class Musician (models.Model):<br/> first_name = models.CharField(max_length= 50 )<br/> last_name = models.CharField(max_length= 50 )<br/> instrument = models.CharField(max_length= 100 )<br/> <br/> class Album (models.Model):<br/> artist = models.ForeignKey( Musician )<br/> name = models.CharField(max_length= 100 )<br/> release_date = models.DateField()<br/> num_stars = models.IntegerField() <br/>
Field types
Each field in your model must be an instance of the corresponding
Field class. Django uses Field class types to define several things:
- The type of database column (for example, INTEGER, VARCHAR ).
- A widget to use the Django admin interface if you want to use it (for example, input type = "text"> ).
- The minimum verification requirements used in the admin interface are http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.__unicode__tor of Django and automatically-generated forms.
Django comes with dozens of built-in types; You can see the full list in the
help for the model fields . You can also create
your own field types .
Field parameters
Each field has a specific set of characteristic arguments that are documented in the
help for the model fields . For example,
CharField (and its subclasses) requires the
max_length argument, which specifies the size of the
VARCHAR database field used to store your data.
docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.__unicode__There is also a set of generic arguments that are available for all field types. All of them are optional. The full description is provided in the
help , however we will give a short overview of the most frequently used ones here:
nullIf the value of the argument is True , Django will store null values ​​in the database as NULL . Default is False .
blankIf the value of the argument is True , then the field may be empty. Default is False .
Note that blank is different to null . The null argument applies only to databases, and blank, in turn, is responsible for validating input. If the field has a blank argument with a value of TRUE , then the check in docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.default Django administrator interface will allow the input of an empty value. If the attribute value is False , the field will be http: //docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.default is required.
choicesAn iterator (for example, a list or a tuple) consisting of several pairs of values ​​used as choices for this field. If the argument is specified, the Django administrator interface will use the drop-down list instead of the usual input field and limit the choices to those specified.
The list of choices looks like this:
<docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.__unicode__
a href="http://sc.me/3744/s">Copy Source | Copy HTML <br/>YEAR_IN_SCHOOL_CHOICES = (<br/> (u 'FR' , u 'Freshman' ),<br/> (u 'SO' , u 'Sophomore' ),<br/> (u 'JR' , u 'Junior' ),<br/> (u 'SR' , u 'Senior' ),<br/> (u 'GR' , u 'Graduate' ),<br/>) <br/>
The first element in each tuple is the value that will be stored in the database, the second will be displayed in the administrator interface or in
ModelChoiceField . When using an instance of a model object, the output of
choices for a field can be made using the
get_FOO_display method. For example:
Copy Source | Copy HTML<br/> from django.db import models<br/> <br/> class Person (models.Model):<br/> GENDER_CHOICES = (<br/> (u 'M' , u 'Male' ),<br/> (u 'F' , u 'Female' ),<br/> )<br/> name = models.CharField(max_length= 60 )<br/> gender = models.CharField(max_length= 2 , choices=GENDER_CHOICES) <br/>
Copy Source | Copy HTML<br/>>>> p = Person(name= "Fred Flinstone" , gender= "M" )<br/>>>> p.save()<br/>>>> p.gender<br/>u 'M' <br/>>>> p.get_gender_display()<br/>u 'Male' <br/>
defaultThe value of this argument is the value of the default field or the called object (in this case, the called object will be called each time a new object is created).
help_textThe value of this argument is the text (additional “help”), which will be displayed in the administrator interface in the form of objects after this field. This is useful for documenting if your object does not have an administrator form.
primary_keyIf the value of the argument is True , the field will be the primary key for the model.
If you do not define primary_key = True for any of the fields in your model, Django will automatically add IntegerField so that the model has a primary key, so you can set the value primary_key = True for any field only if you want to override the standard behavior when creating the primary key .
uniqueIf the argument is True , the field must be unique across the entire database table.
Recall, the above is only a brief description of frequently used field arguments. You can learn more about them
here .
Automatic primary keys
By default, Django adds the following field to each model:
Copy Source | Copy HTML<br/>id = models.AutoField(primary_key=True) <br/>
This is an auto-increment primary key.
If you want to create your primary key, simply define the attribute
primary_key = True in one of your fields. If Django sees that you have uniquely specified
Field.primary_key , then the
id column will not be added.
Each model should have one and only one field with
primary_key = True .
Detailed field names
Each field type, except for
ForeignKey ,
ManyToManyField and
OneToOneField , contains an optional first argument - a verbose name. If this argument is not specified, Django will automatically create it using field attribute names, converting underscores to spaces.
In this example, the detailed name will be
“Person's first name” :
Copy Source | Copy HTML<br/>first_name = models.CharField( "Person's first name" , max_length= 30 ) <br/>
In this example, the detailed name will be
“first name” :
Copy Source | Copy HTML<br/>first_name = models.CharField(max_length= 30 ) <br/>
ForeignKey ,
ManyToManyField and
OneToOneField require that the model be the first argument, so the
verbose_name argument is used with them:
Copy Source | Copy HTML<br/>poll = models.ForeignKey(Poll, verbose_name= "the related poll" )<br/>sites = models.ManyToManyField(Site, verbose_name= "list of sites" )<br/>place = models.OneToOneField(Place, verbose_name= "related place" ) <br/>
It is not customary to set the value of
verbose_name with a capital letter. Django will automatically translate the first character to upper case when required.
That's all :)
to be continuedTranslation Django Documentation: Models. Part 2Translation Django Documentation: Models. Part 3Translation Django Documentation: Models. Part 4 (Last)