models :URL---->http://www.cnblogs.com/wupeiqi/p/6216618.html
null Whether the field in the database can be null
do_column The column name of the field in the database
default The default value of the field in the database
primary_key Whether the field in the database is the primary key
Whether the fields in the db_index database can be indexed
Whether the field in the unique database can be uniquely indexed
Whether the field [date] in the unique_for_data database can be uniquely indexed
unique_for_month Whether the field [month] in the database can be uniquely indexed
unique_for_year Whether the field [year] in the database can be uniquely indexed
verbose_name Field name displayed in Admin
Whether to allow user input to be blank in blank Admin
Is it possible to edit in editable Admin
The prompt information of this field in help_test Admin
The content of the selection box in the choices Admin is stored in memory with unchanged data to avoid cross-table operations
Such as: gf = models.IntegerField(choices=[(0, 'He Sui'),(1, 'Big cousin'),],default=1)
error_messages Customize the error message {dictionary type}, customize the error message you want to display
字典键:null, blank, invalid, invalid_choice, unique, and unique_for_data
{'null':"Cannot be null",'invalid':"Format error"}
#custom auto increment column
nid = models.AutoFieid(primary_key=True)
#metainformation
class UserInfo(models.Model):
nid = models.AutoFieid(primary_key=True)
username = models.Charfiled(max_length=32)
class Meta:
db_table = "table_name" #The name of the table generated in the database, default: app name + underscore + class name
index_together = {("pub_date","deadline"),} #Joint index
unique_together = (("pub_date","deadline"),) #Joint unique index
vernose_name = "username" #Table name displayed in Admin
#One-to-many operations:
to, the name of the table to be associated with
to_field=None, the name of the field to be associated, the default is the primary key ID
on_delete=None when deleting data in the associated table
models.CASCADE, delete the associated data, and also delete the associated data
models.DO_NOTHING, raise error integrityError
models.PROTECT, raises error ProtectedError
models.SET_NULL, delete the associated data, and set the associated value to null (premise that the FK field needs to be set to allow null)
models.SET_DEFAULT, delete the associated data, and set the associated value to the default value (premise that the FK field needs to be set to the default value default)
related_name = None, the field name used in the reverse operation, instead of [table name_set]
related_query_name = None, the field name used in the reverse operation, instead of [table name]
limt_choice_to = None, displaying related books in Admin or ModelForm is a condition provided by them
db_constraint=True know whether to create foreign key constraints in the database
patent_link=False whether to show linked data in Adimn
SNAKE:
create(), can accept dictionary type data **kwargs
delete()
get(), get a single piece of data, if it does not exist, an error will be reported (not recommended)
all(), get all data
filter(), get the data of the specified condition
exclude(), get data other than conditions
update() will update the data of the specified condition, supports dictionary type **kwargs
count() gets the number of matches
id__gt=1 Get the value with ID greater than 1
id__gte=1 gets the value with ID greater than or equal to 1
(id__lt=10, id__gt=1), get the value whose ID is greater than 1 and less than 10
id_in[11,22,33,] Get the value with ID equal to 11,22,33,
pub_date__isnull = True
(name__contains="ale") , fuzzy matching
(name__icontains="Ale"), case insensitive
(id__range=[1,2]) #range between, end
(name='seven').order_by('id') #asc
(name='seven').order_by('-id') #desc
group by
from django.db.models import Count, Min, Max, Sum
values('id').annotate(c=Count(num))
models.Tb1.objects.all()[10:20] slice
Entry.objects.get(title__regex=r'^(An?|The) +')
Entry.objects.get(title__iregex=r'^(an?|the) +')
date
Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))
year
Entry.objects.filter(pub_date__year=2005)
Entry.objects.filter(pub_date__month__gte=6)
select_related(self,*fields) #Performance related: perform join list operation between tables to obtain associated data at one time
prefetch_related(self,*lookups) #Performance related: The speed of multi-table join table operation will be slow. Use it to perform multiple SQL queries to implement table join operation in Python code
aggregate() aggregate function, get dictionary type aggregation result
bulk_create() bulk insert
get_field_dispay: return the readable value of the choice field (1, 'success') - return 'success'
get_or_create(self,default) #If it exists, get it, otherwise create it !!!!
update_or_create(self,default) #If exists, update, otherwise create!!!!
first(self) gets the first
last(self) get the last one
in_bulk(self,id_list=None):
id_list = [11,22,33]
exists(self)