mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-10-31 07:57:55 +03:00 
			
		
		
		
	Thanks to Jon Dufresne (@jdufresne) for review. Co-authored-by: Asif Saif Uddin <auvipy@gmail.com> Co-authored-by: Rizwan Mansuri <Rizwan@webbyfox.com>
		
			
				
	
	
		
			41 lines
		
	
	
		
			1015 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1015 B
		
	
	
	
		
			Python
		
	
	
	
	
	
| from django.contrib.contenttypes.fields import (
 | |
|     GenericForeignKey, GenericRelation
 | |
| )
 | |
| from django.contrib.contenttypes.models import ContentType
 | |
| from django.db import models
 | |
| 
 | |
| 
 | |
| class Tag(models.Model):
 | |
|     """
 | |
|     Tags have a descriptive slug, and are attached to an arbitrary object.
 | |
|     """
 | |
|     tag = models.SlugField()
 | |
|     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
 | |
|     object_id = models.PositiveIntegerField()
 | |
|     tagged_item = GenericForeignKey('content_type', 'object_id')
 | |
| 
 | |
|     def __str__(self):
 | |
|         return self.tag
 | |
| 
 | |
| 
 | |
| class Bookmark(models.Model):
 | |
|     """
 | |
|     A URL bookmark that may have multiple tags attached.
 | |
|     """
 | |
|     url = models.URLField()
 | |
|     tags = GenericRelation(Tag)
 | |
| 
 | |
|     def __str__(self):
 | |
|         return 'Bookmark: %s' % self.url
 | |
| 
 | |
| 
 | |
| class Note(models.Model):
 | |
|     """
 | |
|     A textual note that may have multiple tags attached.
 | |
|     """
 | |
|     text = models.TextField()
 | |
|     tags = GenericRelation(Tag)
 | |
| 
 | |
|     def __str__(self):
 | |
|         return 'Note: %s' % self.text
 |