models from django.db Import models ' '' introduced django.db models from the model, means may be designed directly in the application file in the current models that model the application in the database. Each of the models in a database corresponding to database each class corresponds to a table in the class, the impact number and the type of data in the table. design model when writing directly to the class models which all models of models need to inherit the django.db model class, pay attention to start with a capital .Model is a class of models. '' ' # the Create your models here Wallpaper. ' '' in the use of shell functions manage, and want to shell the data model in the database by type of operation. must first introducing specific modes which model classes in the application. then use the class definition model instance objects, the object operation by way of example. may object instance, the specific properties of model classes, the assignment operation. after the operation, need to use the object instance .save () method to save the database in order to bring it into force. can save data save method only instance of an object. If the pass Variable = BookInfo.objects.get (id = 1) obtained by the object is an instance, but not stored directly through the save (), since id is also acquired, and the id of the same instance of an object before the changed id, can save () . storing items following !!!!, id does not automatically skipped, resulting in the coexistence of a plurality id. data after id read when it is post-added >>> booktest.models Import from the BookInfo >>> '' ' # custom manager module generally has two uses, a modified selection set 2 to simplify the operation to add custom module.. class BookInfoManager (models.Manager): ' '' Library management type '' ' # 1 selection set has been modified complete screening of isDelete. DEF All (Self): Books = . Super () All () Books = books.filter (is_delete = False) return Books #2 Create a new method simplifies the operation objects. DEF create_book (Self, BTITLE, bpublish): # 1. Create a book Object # To avoid BookInfo change the class name, resulting in a custom method can not be indexed, # here use the same self.model can be indexed to a reference model for this class Manager method. # in this way, no matter how to change the yuan class, method names do not change. model_class = self.model # book = BookInfo () book = model_class () # 2. Object assignment for the book = book.btitle BTITLE book.bpublish = bpublish # 3. save data book.save () # 4. returns the object return Book # a single type of classBookInfo (models.Model): BTITLE = models.CharField (max_length = 20 ) # Models class, more than there are all kinds of data to create a model of the Model function, and the model you want to use. Bpublish = models.DateField () # associated with self defined Manager Objects = BookInfoManager () # Once a custom manager, must be associated in the model class module or can not have contact. # complete new function within the model class wording, but generally written in the external model class. # 1. . define class method # 2 need to inherit the class itself cls # @classmethod # DEF create_book (cls, BTITLE, bpublish): # obj = cls () # obj.btitle = BTITLE # obj.bpublish = bpublish # obj.save () # # Return obj # To prevent the application name changes, you should define the metaclass Meta develop a database table names. Class Meta: db_table = " booktest " # after the specified model class corresponding to the table name, # regardless of the name of the application how the sound changes # can be linked to the data table. # many types of class HeroInfo (models.Model): # name hname = models.CharField (max_length = 20 ) # gender is boolean boolean # hgender = models.BooleanField (defalut = False) spelling errors = models.BooleanField hgender (default = False) # Remarks arts hcomment = models.CharField (max_length = 128) Hbook = models.ForeignKey ( ' BookInfo ' ) # relationship attributes, and to establish the relationship between BookInfo. Books heroes and many relationship (foreign key). '' ' Relationship attribute in the data table the relationship between the time-to-many when a correspondence relationship, the correspondence relationship is necessary to fill in association by (poly). '' ' ' '' tables associated, is a single object object, when using the hero object to invoke the associated object, can directly suffix, related attribute associated object. For example: >>> h3.hname 'Duan' >>> h3.hbook <the BookInfo: the BookInfo object> >>> h3.hbook.btitle 'Dragon' '' '