| 1 |
|
|---|
| 2 | from django.db import models
|
|---|
| 3 |
|
|---|
| 4 | # Abstract base classes with related models
|
|---|
| 5 | #
|
|---|
| 6 |
|
|---|
| 7 | class Post(models.Model):
|
|---|
| 8 | title = models.CharField(max_length=50)
|
|---|
| 9 |
|
|---|
| 10 | class Attachment(models.Model):
|
|---|
| 11 | post = models.ForeignKey(Post, related_name='attached_%(class)s_set')
|
|---|
| 12 | content = models.TextField()
|
|---|
| 13 |
|
|---|
| 14 | class Meta:
|
|---|
| 15 | abstract = True
|
|---|
| 16 |
|
|---|
| 17 | def __unicode__(self):
|
|---|
| 18 | return self.content
|
|---|
| 19 |
|
|---|
| 20 | class Comment(Attachment):
|
|---|
| 21 | is_spam = models.BooleanField()
|
|---|
| 22 |
|
|---|
| 23 | class Link(Attachment):
|
|---|
| 24 | url = models.URLField()
|
|---|
| 25 |
|
|---|
| 26 | # Now we create a model with a ForeignKeu=y of an abstract class
|
|---|
| 27 |
|
|---|
| 28 | class MyAttachment(models.Model):
|
|---|
| 29 | mine = models.ForeignKey( Attachment )
|
|---|
| 30 |
|
|---|
| 31 | # This fails to validate due to an internal error in related.py (line 605)
|
|---|
| 32 | # fails in any recent trunk (i.e., rev 8506)
|
|---|