Reputation: 2680
I am trying to create a simple event scheduler for my campus, as an exercise while learning Django. I want to define an event, which can be uniquely identified as a set of venue, speaker, and date. My first attempt is to do this using inheritance from multiple abstract base classes. I am still a beginner at coding, so I am not sure if I am doing it the right way. Is there a better way of doing the same thing?
Here is my first approach. Create abstract classes each for venue, speaker, date and then inherit all of them into a class called event. I want to do this way because the same venues and speakers will be used over and over again.
Another approach would be this, but it doesn't seem to work well with venues and speakers which get reused many times:
class Event(models.Model):
speaker=models.CharField(max_length=100)
venue=models.CharField(max_length=400)
date=models.DateTimeField('Event begins at')
def __unicode__(self):
return self.question
(I am not aware of all database techniques either.)
So, is it possible to create a single class that inherits from multiple abstract classes?
Upvotes: 3
Views: 1197
Reputation: 49826
(a) You can have multiple abstract base classes.
(b) If you just need to collect up methods, then you don't have to have these base classes be subclasses of Model
.
(c) Your event should not inherit from speaker etc, unless you want it to individually have all of the data members, and all of the functionality of those base classes. Instead, it should have a ForeignKey
(or one of the other like fields) to an instance of each of those classes. You should use composition when you need to collect data together; you should only use inheritance when you need to customise behaviour.
Upvotes: 3