Django ORM model to build a database table query

django.db Import Models from
#ORM model:
# generate migration scripts to migrate the database migrate makemigrations

Book class (models.Model):
ID = models.AutoField (primary_key = True) #AutoField sub growth, primary_key = True represents the primary key
name = models.CharField (max_length = 200, null = False) # max_length = 200 the maximum length, null = False not be empty
author = models.CharField (MAX_LENGTH = 100, null = False)
. price = models.FloatField (null = False, default = 0) = 0 # default The default value is 0
Shijian = models.DateTimeField (auto_now_add = True ) when it is first added #auto_now_add will automatically get the current time can be used to create time
each call to save the current method is going to be time to update # auto_now, can be used to modify the time
haha = models.CharField (max_length = 200, False = null, db_column = 'xixi')
# db_column this parameter is the name of the fixed field of the database, the default variable names defined
dianhua = models.CharField (max_length = 11, unique = True) # unique = True Description this field is only

def __str __ (self): # print what is returned when
return "<Book: ({name , {author}, {price}})>". format (name = self.name, author = self.author, price = self .price)
class Meta -:
named db_table, = 'table' # fixed database table name, the name is not written is app _ class name
ordering = [ 'field name', 'field name'] # sorted by name field, you can be more Scheduling in, the same as the previous one will be sorted back


# Foreign key: Effect Effect relationship Xingbie xuesheng
class Xingbie (models.Model):
name = models.CharField (= 200 is MAX_LENGTH)

xuesheng class (models.Model):
name = models.CharField (MAX_LENGTH = 100)
xinbie models.ForeignKey = ( "Xingbie", on_delete = models.CASCADE, the related_name = 'xueshnegmen')
#ForeignKey reference table (reference table, on_delete delete references treatment field: after models.CASCADE delete data delete, known as cascading deletes)
# views in the application is bound xhesheng.xingbei = xinbei like
xinbie1 = models.ForeignKey ( "Xingbie", on_delete models.PROTECT =)
# delete the reference treatment field: models.PROTECT table if there are other applications of your data, you can not delete the protected
xinbie2 = models.ForeignKey ( "Xingbie", on_delete = models.SET_NULL)
# processing reference deleted fields: reference field is empty after deletion after models.SET_NULL, but only if the table's fields can be empty
xinbie3 = models.ForeignKey ( "Xingbie", on_delete = models.SET_DEFAULT, default = Xingbie.objects. (PK = 2)) GET
# delete the reference treatment field: models.After SET_DEFAULT deleted after the data referenced to a default number of data as a reference, such as the default primary key is the id data 2
xinbie4 = models.ForeignKey ( "Xingbie", on_delete = models.SET (Xingbie.objects.get (pk = 2)))
calls the function after deleted after models.SET () reference data: # delete references treatment field get the return value as a default value, may be a function name, no ()
xinbie5 = models.ForeignKey ( "Xingbie", on_delete = models.DO_NOTHING)
# delete the reference treatment field: do nothing after models.DO_NOTHING delete all look at the database level constraint

# Table relationships
# 1-to-many:
# If you want to query the male sex is for all students:
#xingbie = Xingbie.objects.get (name = 'M')
# Xingbie.xuesheng_set.all () so you can get all that boys data
# If you specify related_name in the ForeignKey = 'xueshnegmmen' the attribute name then you can write
# Xingbie.xueshnegmmen.all () so you can get data for all the boys

# If you specify related_name in the ForeignKey = 'xueshnegmmen' The attribute name
# new multi-table data when you can not call seve method, but it is recommended only with the prior method
# xingbie.xueshneg.add (xueshneg, bulk = False )


# 2-one:.
Yiduiyi = models.OneToOneField ( 'table', on_delete = models.CASCADE)

# 3-many:.
Class the Tag (models.Model):
name = models.CharField (MAX_LENGTH = 100)
duoduiduo = models.ManyToManyField ( 'table')

# Inquiry
# View SQL: xxx = Tag.objects.get (field = value) or xxx = Tag.objects.filter (field __exact = value) can only querySet Print (xxx.query)
# when the value is None, correspondence the database is null
#contains: XXX = Tag.objects.filter (field __contains = value) corresponding to the case-sensitive SQL query is fuzzy binary like
#icontains: XXX = Tag.objects.filter (field __icontains = value) case-insensitive corresponds SQL query is fuzzy like
#IN: XXX = Tag.objects.filter (field __in = [value 1, value 2 and value 3]) is equivalent to the SQL xxx in (value 1, value 2, value. 3)
#gt: XXX = Tag.objects.filter (field __gt = value) query field is greater than the value of the data query
#gte: xxx = Tag.objects.filter (field __gte = value) less than or equal queried field data query value
#lt: xxx = Tag.objects.filter (field __gt = value) query field is greater than the value of the data query
#lte: xxx = Tag.objects.filter (field __gte = value) query field is greater than the value equal to query data
#startswith: xxx = Tag.objects.filter (field __startswith = 'xxx') queries all the information beginning xxx, case sensitive
#istartswith: xxx = Tag.objects.filter (field __istartswith = 'xxx') queries all the information beginning xxx, case-insensitive
#endswith: xxx = Tag.objects.filter (field __endswith = 'xxx' ) information to all inquiries xxx ending, case sensitive
#iendswith: xxx = Tag.objects.filter (field __iendswith = 'xxx') to check out all the information xxx ending, case-insensitive
time query range, date , yesr
from datetime datetime # Import time comes
from django.utils.timezone import make_aware # waking hours
#start_time = datetime (year = 2020, month = 1, day = 4, hour = 15, minute = 0, second = 0)
#end_time = datetime (= 2020 year, month The =. 1,. 4 = Day, hour =. 19, = 0 minute, SECOND = 0)
#start_time = make_aware (datetime (= 2020 year, month The =. 1,. 4 = Day, 15 = hour, minute = 0, SECOND = 0))
#end_time = make_aware (datetime (= 2020 year, month The =. 1,. 4 = Day, hour =. 19, = 0 minute, SECOND = 0))
#range: xxx = Tag.objects.filter (field __range = (start_time, end_time)) queries the start time to end time data corresponding to sql where between the start time and end time
#date: xxx = Tag.objects.filter (field __date = datetime (year = 2020, month = 1, day = 4)) data query time is the time, but what you want to configure the database
download file location: http://dev.masql.com/dpwnloads/timezones .html timezone_2018_posix.zpt -POSIX standar download files are copied to the data in masql mysql, the database restart
#year: xxx = Tag.objects.filter (field __year = 2020) query data 2020, and may be greater than or equal with the other queries such as: field = 2020 __year__gte
#month and day and year as
#week_day: xxx = Tag.objects.filter (field __week_day = 4) data query week 3, the rules django is 1 is for Sunday, 2-6 Monday to Friday on behalf of
data query time #time # xxx = Tag.objects.filter (field __time__range = (start_time, end_time)) , data inventory seconds decimals

#isnull: xxx = Tag.objects.filter (field _ _isnull = True) query the data field is empty sq l: where the field is null
#isnull: xxx = Tag.objects.filter (field __isnull = False) a query data field is not empty

regex query:
#regex: XXX = Tag.objects.filter (field __regex = r "^ Expression ") queries the database according to the regular expression, case sensitive
#iregex: xxx = Tag.objects.filter (field __iregex = r" ^ expression ") queries the database according to the regular expression, case-insensitive

Guess you like

Origin www.cnblogs.com/Mr-Simple001/p/12171864.html