Models of the relevant knowledge
1. Autofield: The self-increment integer type. An int field that grows based on ID2. Integerfield: Integer type3. Bigintegerfield: Large integer type. When used in large numbers, similar to Integerfield4. Smallintegerfield: Small integer type. The range of values is small, limited by the database5. Booleanfield: Boolean value (true/false) type. 6. Charfield: String type. 7. Commaseparatedinterfield: Storing an integer sequence with a comma interval8. Datefield: Date type. Datetime.date instances9. Datetimefield: Date type. Datetime.datetime instances10. Emailfield: String type (regular expression mailbox)11. Filefield: File type. 12. Filepathfield: File type. A collection of all the file names in a directory13. Floatfield: Floating-point type14. ImageField: Picture File typeThe. Ipaddressfield:ip address type. String form (such as"192.0.2.30") indicatesGenericipaddressfield:ip address type. String form (such as"192.0.2.30"Or"2a02:42fe::4") indicates17. Nullbooleanfield: Similar to Booleanfield. Allowed value is null18. Positiveintegerfield: Similar to Integerfield. field value must be non-negative19. Positivesmallintegerfield: Similar to Positiveintegerfield, but with a smaller range of values, limited by database settings. 20. Slugfield: Short label. Used primarily to automatically populate Slug fields based on values from other fields21st. TextField: Similar to Charfield. Used to store text. 22. Timefield: Time type. Datetime.time instances23. The Urlfield:url type. 24. Uuidfield: Unique identification type. 25. Binaryfield: Binary Type26. Decimal: Decimal Decimal type27. ForeignKey: foreign Key type28. Manytomanyfield: Many-to-many typesOnetoonefield: One-to-one type
Field options
1. null=True If the field in the database can be empty2. blank=True If null values are allowed when adding data to the Django Admin3. Primary_key =False primary key, after setting the primary key for Autofield, replaces the original self-increment ID column4. Auto_now and Auto_now_add Auto_now automatically created---whether added or modified, is the time of the current operation Auto_now_add automatically created---always at the time of Creation5. Choicesgender_choice=((U'M', u'Male'), (U'F', u'Female'),) gender= Models. Charfield (max_length=2,choices =Gender_choice)6. Max_length7. Default Defaults8the display name of the field in the. Verbose_name admin9. name|field names in the Db_column databaseTen. unique=True does not allow duplicatesOne. Db_index =True Database IndexEditable=.True If the admin is editableError_messages=.None Error HintAuto_created=.False to automatically create15. Help_text tips Help information in adminValidators=.[] Custom Error validation (list type)Upload-to where to upload and more use with Image,filepath
Values () and, vlue_list () and All ()
# User for Models class
The. All () is the data that gets all the columns, which can be added. VALUES () takes out a column, each element is a dictionary:
obj= Model. User.objects.filter (name='Alex'). VALUES ('ID','Email')
# Equivalent to SQL statement: Select ID, email from user where name= ' Alex '
Querysetclass of Python,django [{'ID': 1},{'ID': 2},]values_list (), gets the element to be a tuple, or you can add multiple parameters to get multiple columns:
obj= Model. User.objects.filter (name='Alex'). Value_list ('ID','Email')
equivalent to SQL statement: Select ID, email from user where name= ' Alex '
QuerysetClass of Python,django [(1,'[email protected]'), (2,'[email protected]'),]
Table Basic Operations
# Add models. User.objects.create (id= ' xx ', name= ' xx ') adds a piece of data that can accept dictionary type data * *kwargsobj = models. User (id= ' xx ', name= ' xx ') obj.save () # Check models. User.objects.get (id=123) # Gets a single piece of data that does not exist then an error (not recommended) models. User.objects.all () # gets all data models. User.objects.filter (Name= ' seven ') # gets the data for the specified condition # Delete models. User.objects.filter (Name= ' seven '). Delete () # Deletes the data for the specified condition # change models. User.objects.filter (Name= ' seven '). Update (gender= ' 0 ') # Updates the data for the specified condition and supports **kwargsobj = models. User.objects.get (id=1) obj.name = ' Delav '
Double Dash operation
Usage: # field name + __ + keyword
__exactExactly equals like'AAA'__iexactExact equals ignore case ilike'AAA'__containsInclude like'%aaa%'__icontainsContains ignore case ilike'%aaa%', but for SQLite, the effect of contains is equivalent to Icontains. __GTGreater than__gtegreater than or equal__ltless than__lteless than or equal__inexists within a list range__startswithto have a. Start__istartswithto have a. Start ignoring case__endswithto have a. End__iendswithto have a. End, ignoring case__rangein the ... Within range__yearyear of the date field__monthmonth of the date field__dayDay of the date field__isnull=true/false
#Get numberModels. User.objects.filter (name='Seven'). Count ()#greater than, less thanModels. User.objects.filter (Id__gt=1)#get a value with an ID greater than 1Models. User.objects.filter (id__lt=10)#get a value with an ID of less than 10Models. User.objects.filter (id__lt=10, Id__gt=1)#get a value that has an ID greater than 1 and less than 10 #inchModels. User.objects.filter (Id__in=[11, 22, 33])#get data with ID equal to 11, 22, 33Models. User.objects.exclude (Id__in=[11, 22, 33])# not in #containsModels. User.objects.filter (name__contains="Ven"# Name contains the VEN data models. User.objects.filter (Name__icontains="Ven")#icontains case insensitiveModels. User.objects.exclude (name__icontains="Ven"# Name does not contain VEN data#RangeModels. User.objects.filter (Id__range=[1, 3])#range between and #other similarStartswith,istartswith, EndsWith, Iendswith,#Order byModels. User.objects.filter (name='Seven'). Order_by ('ID')#ASCModels. User.objects.filter (name='Seven'). Order_by ('-id')#desc #limit, offsetModels. User.objects.all () [10:20] #Group by fromDjango.db.modelsImportCount, Min, Max, Summodels.User.objects.filter (C1=1). VALUES ('ID'). Annotate (C=count ('Num'))
Django Models Related