| 1 |
from django.contrib.contenttypes.fields import ( |
| 2 |
GenericForeignKey, GenericRelation |
| 3 |
) |
| 4 |
from django.contrib.contenttypes.models import ContentType |
| 5 |
from django.db import models |
| 6 |
|
| 7 |
|
| 8 |
class Tag(models.Model): |
| 9 |
""" |
| 10 |
Tags have a descriptive slug, and are attached to an arbitrary object. |
| 11 |
""" |
| 12 |
tag = models.SlugField() |
| 13 |
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) |
| 14 |
object_id = models.PositiveIntegerField() |
| 15 |
tagged_item = GenericForeignKey('content_type', 'object_id') |
| 16 |
|
| 17 |
def __str__(self): |
| 18 |
return self.tag |
| 19 |
|
| 20 |
|
| 21 |
class Bookmark(models.Model): |
| 22 |
""" |
| 23 |
A URL bookmark that may have multiple tags attached. |
| 24 |
""" |
| 25 |
url = models.URLField() |
| 26 |
tags = GenericRelation(Tag) |
| 27 |
|
| 28 |
def __str__(self): |
| 29 |
return 'Bookmark: %s' % self.url |
| 30 |
|
| 31 |
|
| 32 |
class Note(models.Model): |
| 33 |
""" |
| 34 |
A textual note that may have multiple tags attached. |
| 35 |
""" |
| 36 |
text = models.TextField() |
| 37 |
tags = GenericRelation(Tag) |
| 38 |
|
| 39 |
def __str__(self): |
| 40 |
return 'Note: %s' % self.text |