mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	newforms-admin: Merged from trunk up to [7766].
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7770 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
		| @@ -38,6 +38,29 @@ class Student(CommonInfo): | ||||
|     class Meta: | ||||
|         pass | ||||
|  | ||||
| # | ||||
| # Abstract base classes with related models | ||||
| # | ||||
|  | ||||
| class Post(models.Model): | ||||
|     title = models.CharField(max_length=50) | ||||
|  | ||||
| class Attachment(models.Model): | ||||
|     post = models.ForeignKey(Post, related_name='attached_%(class)s_set') | ||||
|     content = models.TextField() | ||||
|  | ||||
|     class Meta: | ||||
|         abstract = True | ||||
|  | ||||
|     def __unicode__(self): | ||||
|         return self.content | ||||
|  | ||||
| class Comment(Attachment): | ||||
|     is_spam = models.BooleanField() | ||||
|  | ||||
| class Link(Attachment): | ||||
|     url = models.URLField() | ||||
|  | ||||
| # | ||||
| # Multi-table inheritance | ||||
| # | ||||
| @@ -128,9 +151,25 @@ Traceback (most recent call last): | ||||
|     ... | ||||
| AttributeError: type object 'CommonInfo' has no attribute 'objects' | ||||
|  | ||||
| # The Place/Restaurant/ItalianRestaurant models, on the other hand, all exist | ||||
| # as independent models. However, the subclasses also have transparent access | ||||
| # to the fields of their ancestors. | ||||
| # Create a Post | ||||
| >>> post = Post(title='Lorem Ipsum') | ||||
| >>> post.save() | ||||
|  | ||||
| # The Post model has distinct accessors for the Comment and Link models. | ||||
| >>> post.attached_comment_set.create(content='Save $ on V1agr@', is_spam=True) | ||||
| <Comment: Save $ on V1agr@> | ||||
| >>> post.attached_link_set.create(content='The Web framework for perfectionists with deadlines.', url='http://www.djangoproject.com/') | ||||
| <Link: The Web framework for perfectionists with deadlines.> | ||||
|  | ||||
| # The Post model doesn't have an attribute called 'attached_%(class)s_set'. | ||||
| >>> getattr(post, 'attached_%(class)s_set') | ||||
| Traceback (most recent call last): | ||||
|     ... | ||||
| AttributeError: 'Post' object has no attribute 'attached_%(class)s_set' | ||||
|  | ||||
| # The Place/Restaurant/ItalianRestaurant models all exist as independent | ||||
| # models. However, the subclasses also have transparent access to the fields of | ||||
| # their ancestors. | ||||
|  | ||||
| # Create a couple of Places. | ||||
| >>> p1 = Place(name='Master Shakes', address='666 W. Jersey') | ||||
|   | ||||
		Reference in New Issue
	
	Block a user