# Coding=utf-8from Django.db Import Models "" "Django Database Relationship: one-to-one relationship: Onetoonefield Many-to-many relationships: Manytomanyfield-to-a relationship: ForeignKey" "# # # One-to-one Relationshipsclass place (models. Model): name = models. Charfield (max_length=50) address = models. Charfield (MAX_LENGTH=80) def __str__ (self): # __unicode__ on Python 2 return '%s The place '% self. Nameclass Restaurant (models. Model): Place = models. Onetoonefield (place, primary_key=true) Serves_hot_dogs = models. Booleanfield (default=false) Serves_pizza = models. Booleanfield (Default=false) def __str__ (self): # __unicode__ on Python 2 return '%s the restaurant ' % self.place.name## CREATE TABLE ' Db_place ' (# # ' id ' int (one) not NULL auto_increment,## ' name ' varchar (a) Not null,# # ' address ' varchar not null,## PRIMARY KEY (' id ') # #) Engine=innodb DEFAULT charset=utf8#### CREATE TABLE ' db_res Taurant ' (# # ' place_id ' int (one) not null,## ' serves_hot_dogs ' tinyint (1) Not null,## ' Serves_pizza 'tinyint (1) Not null,## PRIMARY KEY (' place_id '), # # CONSTRAINT ' db_restaurant_place_id_606d40e1_fk_db_place_id ' FOREIGN KEY (' place_id ') REFERENCES ' db_place ' (' id ') # #) Engine=innodb DEFAULT Charset=utf8class Waiter (models. Model): Restaurant = models. ForeignKey (Restaurant) name = models. Charfield (MAX_LENGTH=50) def __str__ (self): # __unicode__ on Python 2 return '%s The waiter at%s ' % (Self.name, self.restaurant) # CREATE TABLE ' Db_waiter ' (# ' id ' int (one) not NULL auto_increment,# ' name ' varchar (50) Not null,# ' restaurant_id ' int (one) not null,# PRIMARY key (' id '), # key ' db_waiter_ee9d9d3e ' (' restaurant_id '), # CO Nstraint ' db_waiter_restaurant_id_7b6c7331_fk_db_restaurant_place_id ' FOREIGN KEY (' restaurant_id ') REFERENCES ' db_ Restaurant ' (' place_id ') #) Engine=innodb DEFAULT charset=utf8## >python manage.py syncdb# Operations to perform:# Ap Ply all Migrations:admin, ContentTypes, auth, sessions# Running migrations:# applying ContenttypEs.0001_initial ... ok# Applying auth.0001_initial ... ok# Applying admin.0001_initial ... ok# Applying sessions.0001_initial ... ok## you have installed Django's auth system, and don ' t have any superusers defined.# would what do I like to create a now? (yes/no): yes# Username (leave blank to use ' Zhangsan '): admin# Email Address: [email protected]# password:*****# Pas Sword (again):* ****# Superuser created successfully.## >python manage.py makemigrations# migrations for ' db ': # 0001_i nitial.py:#-Create Model place#-Create model restaurant#-Create model waiter## >python manage.py MIGR ate# Operations to perform:# apply all migrations:admin, contenttypes, DB, auth, sessions# Running migrations:# apply ing db.0001_initial ... ok## many-to-many Relationshipsclass Publication (models. Model): title = models. Charfield (MAX_LENGTH=30) def __str__ (self): # __unicode__ on Python 2 return Self.title class Me ta:ordering = (' title ',) cLass article (models. Model): Headline = models. Charfield (max_length=100) publications = models. Manytomanyfield (Publication) def __str__ (self): # __unicode__ on Python 2 return self.headline C Lass meta:ordering = (' headline ',) # # CREATE TABLE ' Db_publication ' (# # ' id ' int (one) not NULL auto_increment,## ' title ' varchar (+) not null,## PRIMARY KEY (' id ') # #) Engine=innodb DEFAULT charset=utf8###### CREATE TABLE ' db_articl E ' (# # ' id ' int (one) not NULL auto_increment,## ' headline ' varchar (+) not null,## PRIMARY KEY (' id ') # #) Engine=inn ODB DEFAULT charset=utf8#### CREATE TABLE ' Db_article_publications ' (# # ' id ' int (one) not NULL auto_increment,## ' Artic le_id ' int (one) not null,## ' publication_id ' int (one) not null,## PRIMARY KEY (' id '), # # UNIQUE KEY ' article_id ' (' Arti cle_id ', ' publication_id '), # # key ' db_article_publications_a00c1b00 ' (' article_id '), # # key ' Db_article_publications_ 72ef6487 ' (' publication_id '), # # CONSTRAINT ' db_article_publicat_publication_id_407fcd4d_fk_db_publication_id ' FOREIGN KEY (' publication_id ') REFERENCES ' db_ Publication ' (' ID '), # # CONSTRAINT ' db_article_publications_article_id_b757f51_fk_db_article_id ' FOREIGN KEY (' article_id ') REFERENCES ' db_article ' (' id ') # #) Engine=innodb DEFAULT charset=utf8# >python manage.py makemigrations# Migrations for ' db ': # 0002_auto_20141013_1311.py:#-Create Model article#-Create model publication#-ADD Field publications to article## >python manage.py migrate# Operations to perform:# Apply all migrations:admin, cont Enttypes, DB, auth, sessions# Running migrations:# applying db.0002_auto_20141013_1311 ... ok## many-to-one Relationshipsclass Reporter (models. Model): First_Name = models. Charfield (max_length=30) last_name = models. Charfield (max_length=30) email = models. Emailfield () def __str__ (self): # __unicode__ on Python 2 return '%s%s '% (Self.first_name, SELF.L Ast_name) class Articler (modEls. Model): Headline = models. Charfield (max_length=100) pub_date = models. Datefield () Reporter = Models. ForeignKey (Reporter) def __str__ (self): # __unicode__ on Python 2 return Self.headline class Met a:ordering = (' headline ',) # # CREATE TABLE ' Db_reporter ' (# # ' id ' int (one) not NULL auto_increment,## ' First_nam E ' varchar (+) not null,## ' last_name ' varchar (+) not null,## ' email ' varchar (*) not null,## PRIMARY KEY (' id ') # # ) Engine=innodb DEFAULT charset=utf8## # # CREATE TABLE ' Db_articler ' (# # ' id ' int (one) not NULL auto_increment,## ' head Line ' varchar (+) not null,## ' pub_date ' date not null,## ' reporter_id ' int (one) not null,## PRIMARY KEY (' id '), # # KEY ' db_articler_947bdf92 ' (' reporter_id '), # # CONSTRAINT ' db_articler_reporter_id_26a49a33_fk_db_reporter_id ' FOREIGN KEY (' reporter_id ') REFERENCES ' db_reporter ' (' id ') # #) Engine=innodb DEFAULT charset=utf8# >python manage.py m akemigrations# migrations for ' db ': # 0003_auto_20141013_1318.py:#-Create Model articler#-Create model reporter#-Add field Reporter to articler## >python manage.py migrate# Operations to perform:# Apply all migrations:admin, contenttypes, DB, auth, sessions# Ru Nning migrations:# Applying db.0003_auto_20141013_1318 ... Ok
SOURCE Download: Http://git.oschina.net/gitlab/StartWithCoding/tree/master/example/django/django_db_relationships
Django DB Relationship