class School (models. Model): = models. Onetoonefield ('Teacher', unique=False)class Teacher (models. Model): = models. Charfield (max_length=10)
one to one
class School (models. Model): = models. ForeignKey ('Teacher', unique=False) = models. Charfield (max_length=10) = models. Charfield (max_length=10)class Teacher (models. Model): = models. Charfield (max_length=10)
One -to-many
#No index createdclassHosttogroup (models. Model): Hgid= Models. Autofield (primary_key=True) host_id= Models. ForeignKey ('Host') grou_id= Models. ForeignKey ('Group') classHost (models. Model): HID= Models. Autofield (primary_key=True) hostname= Models. Charfield (max_length=32) IP= Models. Charfield (max_length=32)classGroup (models. Model): Gig= Models. Autofield (primary_key=True) name= Models. Charfield (MAX_LENGTH=16)
traditional Many-to-many
class Host (models. Model): = models. Autofield (primary_key=True) = models. Charfield (max_length=32) = models. Charfield (max_length=32)class Group (models. Model): = models. Autofield (primary_key=True) = models. Charfield (max_length=16) = models. Manytomanyfield ('Host') #
Django Many-to-many
classHost (models. Model): HID= Models. Autofield (primary_key=True) hostname= Models. Charfield (max_length=32) IP= Models. Charfield (max_length=32)classGroup (models. Model): Gig= Models. Autofield (primary_key=True) name= Models. Charfield (max_length=16)#Many-to-many self-customization strong recommendations based on the third table no constraints are made by default,classHosttogroup (models. Model): Hgid= Models. Autofield (primary_key=True) host_id= Models. ForeignKey ('Host') group_id= Models. ForeignKey ('Group') Status=models. Integerfield ()#Add constraint classMeta:#Federated Index #Index_together = ("host_id", ' goup_id ') #Federated Unique IndexUnique_together = [ ('host_id','group_id'),]
Many-to-many (self-built third sheet)
classSingle (models. Model):#self-increment IDAuto = models. Autofield (primary_key=True)#Charfield requirements must have a parameter maxlengthchar = models. Charfield (max_length=32,null=True)#used to hold an integer.INTs = models. Integerfield (null=True)#a Charfield with the legality of checking Email, do not accept maxlength parameters.Emal = models. Emailfield (max_length=32,null=True)#a text field that is large in size.Text = models. TextField (max_length=128,null=True)#used to save the URL. If the verify_exists parameter is true (the default), the given URL is pre-checked for presence (that is, the URL is loaded effectively and does not return a 404 response).URL = models. Urlfield (null=True)## date format Yyyy-mm-ddDate = models. Datefield (null=True)##日期 + time format yyyy-mm-dd Hh:mm[:ss[.uuuuuu]][tz]datetime = models. Datetimefield (null=True)##时间格式 Hh:mm[:ss[.uuuuuu]]Time = models. Timefield (null=True) Floats=models. Floatfield ()#decimal must be max_digits the total number of digits decimal_places the number of decimal digits and the total number of digits is greater than or equal to the decimal placeDecimals = models. Decimalfield (decimal_places=2,null=true,max_digits=10)
Single Table Setup
Django_mysql Data Sheet _ Create