12π
Iβve pondered about both answers and came up with something based off of those suggestions. Thus Iβm adding this answer of my own.
Iβve chosen to use django-polymorphic, quite nice tool suggested by @professorDante. Since this is a multi-table inheritance, @albarβs answer is also somewhat correct.
tl;dr
django-polymorphic attends the 3 main requirements:
- Allow django ORM querying style
- Keep db level constraints by having a multi-table inheritance and one table for each sub class
- Easy django admin integration
Longer version
Django-polymorphic allows me to query all different event instances from the base class, like:
# assuming the objects where previously created
>>> Event.objects.all()
[<SubEventA object>, <SubEventB object>, <SubEventC object>]
It also has great django admin integration, allowing seamless objects creation and editing.
The models using django-polymorphic would look like:
# A.models.py
from polymorphic import PolymorphicModel
class Event(PolymorphicModel):
commom_field = models.BooleanField()
# no longer abstract
class SubEventA(Event):
email = models.EmailField(unique=True)
class SubEventB(Event):
title = models.TextField()
class SubEventC(Event):
number = models.IntegerField(default=10)
# B.models.py
# it doesnt have to be polymorphic to reference polymorphic models
class OtherModel(models.Model):
event = models.ForeignKey('A.Event')
Besides, I can reference only the base model from another class and I can assign any of the subclasses directly, such as:
>>> sub_event_b = SubEventB.objects.create(title='what a lovely day')
>>> other_model = OtherModel()
>>> other_model.event = sub_event_b
4π
My .2c on this. Not sure about your design in #3. Each SubEvent subclasses Event, and has a one-to-one to Event? Isnβt that the same thing?
Your proposal on the Generic Key is exactly what it is designed for.
Another possibility β Polymorphism with Mixins. Use something like Django-polymorphic, so querying returns you the subclass you want. I use this all the time and its super useful. Then make Mixins for attributes that will be reused across many classes. So a simple example, making an email Mixin
class EmailMixin(models.Model):
email = models.EmailField(unique=True)
class Meta:
abstract = True
Then use it
class MySubEvent(EmailMixin, models.Model):
<do stuff>
This way you dont have redundant attributes on subclasses, as you would if they were all in the parent.
- No web processes running Django in heroku
- Datetime Field Received a Naive Datetime
- Can I detect if my code is running on cPython or Jython?
2π
Why not a multi-table inheritance?
class Event(models.Model):
commom_field = models.BooleanField()
class SubEventA(Event):
email = models.EmailField(unique=True)
class SubEventB(Event):
title = models.TextField(blank=True)
class SubEventC(Event):
number = models.IntegerField(default=10)
- Getting a list from Django into Javascript as an array
- Django Generic Foreign keys β Good or Bad considering the SQL performance?
- Logging formatters in django
- South: run a migration for a column that is both unique and not null