1👍
✅
Django has the contenttypes framework so you can name concrete events as you wish:
class EventType(models.Model):
name = models.CharField(max_length=200)
has_additional_fields = models.BooleanField(default=True)
content_type = models.ForeignKey(ContentType)
class Event(models.Model):
data = models.TextField(null=True)
event_type = models.ForeignKey(EventType)
def get_real_model(self):
return self.event_type.content_type.model_class()
def get_concrete_event(self):
return self.event_type.content_type.get_object_for_this_type(pk=self.pk)
class ProjectEvent(Event):
group = models.CharField(max_length=200)
class MeetingEvent(Event):
topic = models.CharField(max_length=200)
class NormalEvent(Event):
pass
Get a list of all events:
Event.objects.all()
Get a list of projects:
ProjectEvent.objects.all()
Get the concrete (“unknown”) event by id:
Event.objects.get(pk=id).get_concrete_event()
Get the real model of the event by id:
Event.objects.get(pk=id).get_real_model()
Get the form to edit concrete event:
from django.forms.models import modelform_factory
event = Event.objects.get(pk=id)
EventForm = modelform_factory(event.get_real_model())
form = EventForm(instance=event.get_concrete_event())
Get the same form then you know event type:
ProjectEventForm = modelform_factory(ProjectEvent)
form = ProjectEventForm(instance=ProjectEvent.objects.get(pk=id))
Source:stackexchange.com