orm field and query parameters and F and Q

Aggregate query:

  You need to import some modules:

    

from django.db.models Import Max, Min, the Sun, Cont, Avg. 

"" " 
keyword is used in aggregate functions required: Aggregate 
Max: maximum data filtering 
Min: minimization 
Sun: find the sum of the same type of data 
AVg: seeking data fields within an average of 
Cont: number of statistical 

grouping Search keyword: Annotate 
"" "

F and Q query:

  Also you need to be imported module F and Q

from django.db.models Import F, Q 

"" " 
example F () can be referenced in the query field, to compare the values of different fields of the same model in two instances; 
F can help us get to the table in a field corresponding values as my filter criteria, rather than I think a custom constant conditions, and to achieve the effect of dynamic comparisons. 

Q queries by default and can be rewritten or 
we can combine & and | operators and the use of parentheses write packet Q arbitrarily complex objects. 
Meanwhile, the object Q ~ can be negated, which allows a combination of normal and inverted query (NOT) query 
query Q can be mixed function objects and keyword parameters. provided to all parameter query function (keyword arguments or Q objects) are "AND" together. However, 
if the Q object appears, it must precede all keyword arguments.
"" "

 

orm fields and parameters:

"" " 

AutoField (Field,) 
        - int auto-increment, must fill in the parameters primary_key = True 

    BigAutoField (AutoField) 
        - BIGINT auto-increment, must fill in the parameters primary_key = True 

        NOTE: If no model when auto-increment, auto It creates a column called id column 
        from django.db Import Models 

        class UserInfo (models.Model): 
            # automatically create a column called id and is an integer increment column 
            username = models.CharField (max_length = 32) 

        class Group (models.Model): 
            # Customization additional 
            NID = models.AutoField (primary_key = True) 
            name = models.CharField (MAX_LENGTH = 32) 

    SmallIntegerField (IntegerField): 
        - small integer -32768 to 32767

    PositiveSmallIntegerField (PositiveIntegerRelDbTypeMixin, IntegerField) 
        - a positive integer smaller 32767 ~ 0 
    IntegerField (Field,) 
        - integer columns (signed) -2147483648 2147483647 ~ 

    PositiveIntegerField (PositiveIntegerRelDbTypeMixin, IntegerField) 
        - ~ 2147483647 positive integers 0 

    BigIntegerField (IntegerField): 
        - Long (signed) -9223372036854775808 ~ 9223372036854775807 

    BooleanField (Field,) 
        - Boolean type 

    NullBooleanField (Field,): 
        - Boolean value may be null 

    as CharField (Field,) 
        - character type 
        - max_length parameters must be provided, max_length characters indicating 

    TextField (Field) 
        - text type 
 
    EmailField (CharField):
        - the type of string, Django Admin and authentication mechanisms provided ModelForm
 
    IPAddressField (Field,) 
        - string type, Django Admin IPV4 and authentication mechanisms provided ModelForm 

    GenericIPAddressField (Field,) 
        - string type, Django Admin provides authentication and ModelForm Ipv4 and Ipv6 
        - parameters: 
            Protocol, specifies Ipv4 or Ipv6, 'both', "IPv4", "IPv6" 
            unpack_ipv4, if designated as True, the input :: ffff: 192.0.2.1 time, may be resolved to 192.0.2.1, this opening function, = Protocol "both" 

    URLField (as CharField) 
        - string type, Django Admin ModelForm provide authentication and the URL 

    SlugField (as CharField) 
        - string type, Django Admin ModelForm provide authentication and support letters, numbers, underscore, hyphen (minus) 

    CommaSeparatedIntegerField (as CharField) 
        - string type digital format must be a comma-separated 

    UUIDField (Field) 
        - string type, Django Admin ModelForm and provides verification of the UUID format 

    FilePathField (Field)
        - string, Django Admin provides functions to read and ModelForm folder file 
        - parameters: 
                path, folder path 
                match = None, regular matching 
                recursive = False, following recursive folder 
                allow_files = True, allows files 
                allow_folders = False , allowing the folder 

    the FileField (Field,) 
        - string stored in the path database, file uploading to the specified directory 
        - parameters: 
            the upload_to = "" save path uploaded file 
            storage = None storage component, the default django.core.files.storage.FileSystemStorage 

    ImageField (FileField) 
        - string path stored in the database, upload files to a specified directory 
        - parameters:
            upload_to = "" path to save the uploaded file 
        - 10 binary decimal
            storage = None storage component, the default django.core.files.storage.FileSystemStorage 
            of width_field = None, upload pictures stored in the database field name height (string) 
            stored in the database field name width height_field = None upload pictures (string) 

    DateTimeField (the DateField) 
        - date + time format of the MM-DD-HH YYYY: the MM [: SS [.uuuuuu]] [the TZ] 

    the DateField (DateTimeCheckMixin, Field,) 
        - date format the MM-DD-YYYY 

    TimeField (DateTimeCheckMixin, Field,) 
        - time format HH: the MM [: SS [.uuuuuu]] 

    DurationField (Field,) 
        - long integer interval stored in the database in accordance with bigint, the ORM is acquired datetime.timedelta type 

    FloatField (Field,) 
        - float 

    DecimalField (Field) 
            max_digits, fractional total length
        - parameters:
            decimal_places, fractional bit length 

    BinaryField (Field,) 
        - binary type 
"" "

 

Custom char fields:

# How custom field type 

class MyCharField (models.Field):
     DEF  __init__ (Self, max_length, * args, ** kwargs): 
        self.max_length = max_length
         # recall the parent class's 
        Super (). __Init__ (max_length = max_length , * args, ** kwargs) 


    DEF db_type (Self, Connection):
         return  ' char (% S) ' % self.max_length

 

Introduction to transactions in orm: 

"" "    The ACID   " "" 

"" "   
Atomic: atomicity refers to all operations in the transaction included either all succeed, or all fail rolled back, the operator of the transaction if successful, it must be fully applied to the database if the operation fails can not have any impact on the database. 
If the user has completed the updates to the database in a transaction when all updates to the outside world must be visible, or no update. the former said the transaction has been submitted, which said the transaction undo ( . or abortion) 
the DBMS must ensure the successful completion of the transaction submitted by manipulating all have full reflected in the database, and the database failed transaction did not affect consistency: consistency means that the transaction must transform the database from one consistent state consistent state to another, that is to say before and after a transaction execution and implementation must be in a consistent state. consistent processing database protection for all semantic constraints. If the state of the database satisfies all integrity constraints, said the database is consistent. For example, when the database is in a consistent state Sl,
Execute a transaction on the database, the database is assumed that during the execution of the transaction state is inconsistent, when executed at the end of a transaction, the database is in a consistent state S2 Isolation: Isolation is when multiple users concurrent access to the database, such as operating with a when the table, a database for each user transaction open, operation not being disturbed by other transactions, for isolation between a plurality of concurrent transactions. the DBMS may provide different levels of separation between transactions executed concurrently. isolated and concurrency there is an inverse relationship between transaction throughput. separability transaction may bring more conflict and more high transaction abortion.
transaction abortion consumes resources that must be accessed again. Therefore, to ensure that high separation level DBMS requires more overhead.
Persistence: Refers to the end as long as the transaction is successful, it updates to the data must be permanently preserved. Even if a system crash occurs, restart the database system, the database can be restored to the state of affairs when a successful conclusion. Update persistence means that when a system or media failure, make sure that committed transactions can not be lost. That update has been submitted to the transaction can be restored. Once a transaction is committed, DBMS must ensure the provision of appropriate redundancy,
making it able to endure the failure of the system. So persistent is to restore the main performance DBMS.
"" "

 

Three database Fan test:  

"" " 


1, a first paradigm (1NF): 
the so-called first paradigm (1NF) refers to the relational model, specifications for an added, all fields should be atomic, i.e., each column of a database table indivisible atomic data items, not a non-atomic set of data items, arrays, records, etc. 
That is, when an attribute of an entity with a plurality of values, must be split into different properties. in the first normal form (1NF ) for each field value in the table is only a property of the entity or a part short of the properties, the first paradigm is no repeat domains. Description: in any relational database, the first paradigm (1NF) the design of relational schema is the basic requirements, general design must meet the first paradigm (1NF). but some relational model in break through the limitation 1NF, which is called non-1NF relational model.
in other words, whether or not must be met 1NF minimum requirements, mainly dependent on the relational model.

2, a second paradigm (2NF) on the basis of 1NF, non-code must be totally dependent on the properties of the candidate codes (non-primary property to eliminate the basis 1NF The main part of the function code dependent) a second paradigm (2NF) is established on the basis of a first normal form (1NF) on, i.e., satisfies the second paradigm (2NF) must first normal form (1NF). The second paradigm ( 2NF) requires that each instance of a database record or table must be uniquely distinguished.
select an attribute or group can distinguish each entity, the entity identified as the only example in the employee ID number table can be realized each employee of a distinction, the ID number is the key candidates, any candidate key can be selected as the primary key. when a candidate can not find the key,
can achieve additional property to distinguish, if the employee relationship, no when storing its ID number, and the name may be repeated at some time the database is running, you can not distinguish entities, such as the provision of design, which do not duplicate ID number to achieve distinction, number or ID is added selected as the primary key.
(add the primary key in the ER is designed to add, feel free to add when not building a database) The second paradigm (2NF) required attributes of the entity entirely dependent on the primary key. The so-called completely dependent on the presence of means can not rely solely on a part of the primary key attribute, if present, and this part of the primary key attribute should be separated to form a new entity,
between the original and the new entity is an entity of many relationship. To achieve the distinction typically requires adding a column to the table, to uniquely identify each instance storage. In short, the second is based on the first paradigm paradigm entirely dependent on the properties of the primary key.

3, a third paradigm (3NF) on the basis of 2NF, any non-primary property is not dependent on other non-primary property (transfer to eliminate dependency on the basis of the 2NF) a third paradigm (3NF) is a second paradigm (2NF) is a subset of , i.e. third normal (3NF) must satisfy the second paradigm (2NF). Briefly, a third paradigm (3NF) requires a relationship comprising a non-primary key information is not already included in other relationships.
For example, there is a department information table, where each department has a department number (dept_id), department name, department briefings and other information. Then the department number listed in the employee information table after the information can no longer with the department about the department name, department, etc. About adding staff information table.
If the department information table does not exist, according to the third normal form (3NF) should build it, otherwise there will be a lot of data redundancy. Briefly, third paradigm is not dependent on other properties of the non-primary property is satisfied on the basis of 2NF, any non-primary property can not be dependent on the primary transfer properties.
"" "

 

 

 

Guess you like

Origin www.cnblogs.com/sweet-i/p/11954134.html