| 1 |
from django.db import models |
| 2 |
|
| 3 |
from polymorphic.models import PolymorphicModel |
| 4 |
from polymorphic.showfields import ShowFieldContent, ShowFieldTypeAndContent |
| 5 |
|
| 6 |
|
| 7 |
class Project(ShowFieldContent, PolymorphicModel): |
| 8 |
"""Polymorphic model""" |
| 9 |
|
| 10 |
topic = models.CharField(max_length=30) |
| 11 |
|
| 12 |
|
| 13 |
class ArtProject(Project): |
| 14 |
artist = models.CharField(max_length=30) |
| 15 |
|
| 16 |
|
| 17 |
class ResearchProject(Project): |
| 18 |
supervisor = models.CharField(max_length=30) |
| 19 |
|
| 20 |
|
| 21 |
class UUIDModelA(ShowFieldTypeAndContent, PolymorphicModel): |
| 22 |
"""UUID as primary key example""" |
| 23 |
|
| 24 |
uuid_primary_key = models.UUIDField(primary_key=True) |
| 25 |
field1 = models.CharField(max_length=10) |
| 26 |
|
| 27 |
|
| 28 |
class UUIDModelB(UUIDModelA): |
| 29 |
field2 = models.CharField(max_length=10) |
| 30 |
|
| 31 |
|
| 32 |
class UUIDModelC(UUIDModelB): |
| 33 |
field3 = models.CharField(max_length=10) |
| 34 |
|
| 35 |
|
| 36 |
class ProxyBase(PolymorphicModel): |
| 37 |
"""Proxy model example - a single table with multiple types.""" |
| 38 |
|
| 39 |
title = models.CharField(max_length=200) |
| 40 |
|
| 41 |
def __unicode__(self): |
| 42 |
return f"<ProxyBase[type={self.polymorphic_ctype}]: {self.title}>" |
| 43 |
|
| 44 |
class Meta: |
| 45 |
ordering = ("title",) |
| 46 |
|
| 47 |
|
| 48 |
class ProxyA(ProxyBase): |
| 49 |
class Meta: |
| 50 |
proxy = True |
| 51 |
|
| 52 |
def __unicode__(self): |
| 53 |
return f"<ProxyA: {self.title}>" |
| 54 |
|
| 55 |
|
| 56 |
class ProxyB(ProxyBase): |
| 57 |
class Meta: |
| 58 |
proxy = True |
| 59 |
|
| 60 |
def __unicode__(self): |
| 61 |
return f"<ProxyB: {self.title}>" |
| 62 |
|
| 63 |
|
| 64 |
# Internals for management command tests |
| 65 |
|
| 66 |
|
| 67 |
class TestModelA(ShowFieldTypeAndContent, PolymorphicModel): |
| 68 |
field1 = models.CharField(max_length=10) |
| 69 |
|
| 70 |
|
| 71 |
class TestModelB(TestModelA): |
| 72 |
field2 = models.CharField(max_length=10) |
| 73 |
|
| 74 |
|
| 75 |
class TestModelC(TestModelB): |
| 76 |
field3 = models.CharField(max_length=10) |
| 77 |
field4 = models.ManyToManyField(TestModelB, related_name="related_c") |
| 78 |
|
| 79 |
|
| 80 |
class NormalModelA(models.Model): |
| 81 |
"""Normal Django inheritance, no polymorphic behavior""" |
| 82 |
|
| 83 |
field1 = models.CharField(max_length=10) |
| 84 |
|
| 85 |
|
| 86 |
class NormalModelB(NormalModelA): |
| 87 |
field2 = models.CharField(max_length=10) |
| 88 |
|
| 89 |
|
| 90 |
class NormalModelC(NormalModelB): |
| 91 |
field3 = models.CharField(max_length=10) |