1. from urllib.parse import quote
    
  2. 
    
  3. from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
    
  4. from django.contrib.contenttypes.models import ContentType
    
  5. from django.contrib.sites.models import SiteManager
    
  6. from django.db import models
    
  7. 
    
  8. 
    
  9. class Site(models.Model):
    
  10.     domain = models.CharField(max_length=100)
    
  11.     objects = SiteManager()
    
  12. 
    
  13. 
    
  14. class Author(models.Model):
    
  15.     name = models.CharField(max_length=100)
    
  16. 
    
  17.     def get_absolute_url(self):
    
  18.         return "/authors/%s/" % self.id
    
  19. 
    
  20. 
    
  21. class Article(models.Model):
    
  22.     title = models.CharField(max_length=100)
    
  23.     slug = models.SlugField()
    
  24.     author = models.ForeignKey(Author, models.CASCADE)
    
  25.     date_created = models.DateTimeField()
    
  26. 
    
  27. 
    
  28. class SchemeIncludedURL(models.Model):
    
  29.     url = models.URLField(max_length=100)
    
  30. 
    
  31.     def get_absolute_url(self):
    
  32.         return self.url
    
  33. 
    
  34. 
    
  35. class ConcreteModel(models.Model):
    
  36.     name = models.CharField(max_length=10)
    
  37. 
    
  38. 
    
  39. class ProxyModel(ConcreteModel):
    
  40.     class Meta:
    
  41.         proxy = True
    
  42. 
    
  43. 
    
  44. class FooWithoutUrl(models.Model):
    
  45.     """
    
  46.     Fake model not defining ``get_absolute_url`` for
    
  47.     ContentTypesTests.test_shortcut_view_without_get_absolute_url()
    
  48.     """
    
  49. 
    
  50.     name = models.CharField(max_length=30, unique=True)
    
  51. 
    
  52. 
    
  53. class FooWithUrl(FooWithoutUrl):
    
  54.     """
    
  55.     Fake model defining ``get_absolute_url`` for
    
  56.     ContentTypesTests.test_shortcut_view().
    
  57.     """
    
  58. 
    
  59.     def get_absolute_url(self):
    
  60.         return "/users/%s/" % quote(self.name)
    
  61. 
    
  62. 
    
  63. class FooWithBrokenAbsoluteUrl(FooWithoutUrl):
    
  64.     """
    
  65.     Fake model defining a ``get_absolute_url`` method containing an error
    
  66.     """
    
  67. 
    
  68.     def get_absolute_url(self):
    
  69.         return "/users/%s/" % self.unknown_field
    
  70. 
    
  71. 
    
  72. class Question(models.Model):
    
  73.     text = models.CharField(max_length=200)
    
  74.     answer_set = GenericRelation("Answer")
    
  75. 
    
  76. 
    
  77. class Answer(models.Model):
    
  78.     text = models.CharField(max_length=200)
    
  79.     content_type = models.ForeignKey(ContentType, models.CASCADE)
    
  80.     object_id = models.PositiveIntegerField()
    
  81.     question = GenericForeignKey()
    
  82. 
    
  83.     class Meta:
    
  84.         order_with_respect_to = "question"
    
  85. 
    
  86. 
    
  87. class Post(models.Model):
    
  88.     """An ordered tag on an item."""
    
  89. 
    
  90.     title = models.CharField(max_length=200)
    
  91.     content_type = models.ForeignKey(ContentType, models.CASCADE, null=True)
    
  92.     object_id = models.PositiveIntegerField(null=True)
    
  93.     parent = GenericForeignKey()
    
  94.     children = GenericRelation("Post")
    
  95. 
    
  96.     class Meta:
    
  97.         order_with_respect_to = "parent"
    
  98. 
    
  99. 
    
  100. class ModelWithNullFKToSite(models.Model):
    
  101.     title = models.CharField(max_length=200)
    
  102.     site = models.ForeignKey(Site, null=True, on_delete=models.CASCADE)
    
  103.     post = models.ForeignKey(Post, null=True, on_delete=models.CASCADE)
    
  104. 
    
  105.     def get_absolute_url(self):
    
  106.         return "/title/%s/" % quote(self.title)
    
  107. 
    
  108. 
    
  109. class ModelWithM2MToSite(models.Model):
    
  110.     title = models.CharField(max_length=200)
    
  111.     sites = models.ManyToManyField(Site)
    
  112. 
    
  113.     def get_absolute_url(self):
    
  114.         return "/title/%s/" % quote(self.title)