# coding=utf-8 from django.db import models """ Django数据库关系: 一对一关系:OneToOneField 多对多关系:ManyToManyField 多对一关系:ForeignKey """ ## One-to-one relationships class 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.name class 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(11) NOT NULL AUTO_INCREMENT, ## `name` varchar(50) NOT NULL, ## `address` varchar(80) NOT NULL, ## PRIMARY KEY (`id`) ## ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ## ## CREATE TABLE `db_restaurant` ( ## `place_id` int(11) 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=utf8 class 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(11) NOT NULL AUTO_INCREMENT, # `name` varchar(50) NOT NULL, # `restaurant_id` int(11) NOT NULL, # PRIMARY KEY (`id`), # KEY `db_waiter_ee9d9d3e` (`restaurant_id`), # CONSTRAINT `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: # Apply 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 you like to create one now? (yes/no): yes # Username (leave blank to use ‘zhangsan‘): admin # Email address: admin@admin.com # Password:***** # Password (again):***** # Superuser created successfully. # # >python manage.py makemigrations # Migrations for ‘db‘: # 0001_initial.py: # - Create model Place # - Create model Restaurant # - Create model Waiter # # >python manage.py migrate # Operations to perform: # Apply all migrations: admin, contenttypes, db, auth, sessions # Running migrations: # Applying db.0001_initial... OK ## Many-to-many relationships class Publication(models.Model): title = models.CharField(max_length=30) def __str__(self): # __unicode__ on Python 2 return self.title class Meta: 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 class Meta: ordering = (‘headline‘,) ## CREATE TABLE `db_publication` ( ## `id` int(11) NOT NULL AUTO_INCREMENT, ## `title` varchar(30) NOT NULL, ## PRIMARY KEY (`id`) ## ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ## ## ## CREATE TABLE `db_article` ( ## `id` int(11) NOT NULL AUTO_INCREMENT, ## `headline` varchar(100) NOT NULL, ## PRIMARY KEY (`id`) ## ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ## ## CREATE TABLE `db_article_publications` ( ## `id` int(11) NOT NULL AUTO_INCREMENT, ## `article_id` int(11) NOT NULL, ## `publication_id` int(11) NOT NULL, ## PRIMARY KEY (`id`), ## UNIQUE KEY `article_id` (`article_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, contenttypes, db, auth, sessions # Running migrations: # Applying db.0002_auto_20141013_1311... OK ## Many-to-one relationships class 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.last_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 Meta: ordering = (‘headline‘,) ## CREATE TABLE `db_reporter` ( ## `id` int(11) NOT NULL AUTO_INCREMENT, ## `first_name` varchar(30) NOT NULL, ## `last_name` varchar(30) NOT NULL, ## `email` varchar(75) NOT NULL, ## PRIMARY KEY (`id`) ## ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ## ## CREATE TABLE `db_articler` ( ## `id` int(11) NOT NULL AUTO_INCREMENT, ## `headline` varchar(100) NOT NULL, ## `pub_date` date NOT NULL, ## `reporter_id` int(11) 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 makemigrations # 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 # Running migrations: # Applying db.0003_auto_20141013_1318... OK
源码下载:http://git.oschina.net/gitlab/StartWithCoding/tree/master/example/django/django_db_relationships