In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the Python Django model". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the Python Django model".
Catalogue
Django model
Field Type
Parameters of field typ
Foreign key
Summary
Django model
The model for Django is defined in the models.py file. The model is the M in MVT, which is also equivalent to the M in MVC.
In Django, the model must inherit from the Model class. For example:
From django.db import models# Create your models here.class BookInfo (models.Model): # A model class will generate a table in the database "" Book Model "" name= models.CharField (max_length=128, verbose_name=' name') # the properties of the class # are the fields in the data table. Pub_date = models.DateField (verbose_name=' release date', null=True) readcount = models.IntegerField (default=0, verbose_name=' readings') commentcount = models.IntegerField (default=0, verbose_name=' comments') is_delete = models.BooleanField (default=False Verbose_name=' logical deletion') class Meta: # class Meta is the fixed writing method db_table = 'bookinfo' # indicates the database table name verbose_name=' Book' # the name displayed in the admin site
Be careful
1. If the model class does not specify a table name, Django defaults to the lowercase app application name _ lowercase model class name as the database table name. Generally, we will specify the database table name through db_table.
2.django creates auto-growing primary key columns for the table, and each model can only have one primary key column. If you set an attribute to the primary key column using the option, django will no longer create an auto-growing primary key column. The default created primary key column property is id, or you can use contention, which means primary key.
3. Double underscores cannot appear in field names because this is one of the query syntax of Django.
Since Django3.2, you can configure the DEFAULT_AUTO_FIELD parameter in settings.py to set the primary key data type. The default is DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField',. Before Django3.2, the default generated primary key data type is AutoField.
Here are the details of the field types commonly used in Django
Field type description BigAutoField auto-growing BigIntegerField, usually not specified. If not specified, Django will automatically create an auto-growing attribute BooleanField Boolean field named id. The value is True or FalseNullBooleanField supports Null, True and False CharField strings. Parameter max_length represents the maximum number of characters in TextField large text field. Generally, IntegerField integer DecimalField decimal floating point is used when more than 4000 characters are used, and parameter max_digits represents the total number of digits. Parameter decimal_places represents the decimal number FloatField floating point number DateField date, and parameter auto_now indicates that each time the object is saved, the field is automatically set to the current time, which is used for the timestamp of "Last modified". It always uses the current date, and the default is False. Parameter auto_now_add indicates that the current time is automatically set when the object is created for the first time. It is used for the creation timestamp. It always uses the current date. The default is False; parameters auto_now_add and auto_now are mutually exclusive. The combination will cause an error TimeField time. The parameter is the same as the DateFieldDateTimeField date time, and the parameter is inherited from FileField with the DateFieldFileField upload file field ImageField. Check the uploaded content to make sure it is a valid image.
Here are the parameters in the field type
The parameter parameter of the field type indicates that if null is True, the default value is Falseblank, the default value is True, the field is allowed to be blank, and the default value is the name of the Falsedb_column field. If not specified, the name of the attribute db_index is used. If the value is True, the index for this field is created in the table. The default value is Falsedefault. If the default value is True, the field will become the primary key field of the model. The default value is False, which is generally used as an option for AutoField. If unique is True, this field must have a unique value in the table. The default value is Falsechoices. This parameter provides options from a series of tuples.
Be careful
The 1.CharField field must specify the parameter max_length
two。 You can also specify the parameter verbose_name for the field, which is mainly used in the admin management page and is actually related to localization. For example, you can specify the parameter verbose_name= "book name" in the name field above, and you will see the book name on the admin administration page.
The 3.null parameter is at the database level. After setting null=True, the field in the database can be empty; the blank parameter is at the form level (HTML). After blank=True, the field can be left empty when the form is filled in.
Foreign key
Foreign keys are usually implemented at the business logic level, not in the database. But usually in the database courses we study, there will be a database design paradigm, and one of the third paradigm is the foreign key constraint. Here is just a brief introduction. Here is another model, which is associated with the previous BookInfo model through foreign keys.
Class PeopleInfo (models.Model): "" personnel model "GENDER_CHOICES = ((0, 'male'), (1,' female')) name= models.CharField (max_length=20, verbose_name=' name') gender = models.SmallIntegerField (choices=GENDER_CHOICES, default=0, verbose_name=' gender') description = models.CharField (max_length=200, null=True, verbose_name=' description') book = models.ForeignKey (BookInfo On_delete=models.CASCADE, verbose_name=' Books') # Foreign key is_delete = models.BooleanField (default=False, verbose_name=' logical deletion') class Meta: db_table = 'peopleinfo' verbose_name=' character information' the following focuses on the choices parameter and models.ForeignKey. The choices parameter takes the value from the GENDER_CHOICES we defined. The first value of the binary will be stored in the database, while the second value will only be displayed in the form. For a model instance, to get the corresponding second value in the field binary, use the get_FOO_display () method. For example, to get the gender information above, you can use the get_gender_display () method. Foreign key: by using models.ForeignKey to set the foreign key, the first parameter of ForeignKey is the name of the model class to be associated, and the second parameter is on_delete. Its common values can be as follows: CASCADE cascading, connecting when deleting the data of the master table, deleting the PROTECT protection of the data in the foreign key table, throwing a ProtectedError exception to prevent the deletion of the data applied by the foreign key in the primary table, setting SET_NULL to NULL, and setting SET_DEFAULT to the default value only if the field null=True is allowed to be null. Only if the default value of this field is set, SET () can be set to a specific value or call a specific method DO_NOTHING to do nothing. If the database indicates cascading, this option will throw an IntegrityError exception.
Note: we need to specify the associated fields in another table when setting foreign keys in the database, but they are not specified in Django. This is because Django specifies the id of another table as the associated field by default. As shown in the following figure:
You can see that the name of the foreign key in the peopleinfo table is book_id
Thank you for your reading, the above is the content of "how to understand the Python Django model", after the study of this article, I believe you have a deeper understanding of how to understand the Python Django model, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.