Created
June 23, 2011 21:38
-
-
Save mhulse/1043698 to your computer and use it in GitHub Desktop.
Basic/simple example of abstract base models and multiple inheritence...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ... | |
| ###################################### | |
| ## | |
| ## Abstract models: | |
| ## | |
| ###################################### | |
| class Base(models.Model): | |
| # http://code.google.com/p/django-tagging/ | |
| tags = TagField() | |
| categories = models.ManyToManyField('Category', blank=True, null=True) | |
| created = models.DateTimeField(_(u'created'), editable=False) | |
| modified = models.DateTimeField(_(u'modified'), editable=False) | |
| # Need this here otherwise tagging will trump with its own manager: | |
| objects = models.Manager() # Admin uses this manager. | |
| class Meta: | |
| abstract = True | |
| def save(self, *args, **kwargs): | |
| if not self.pk: | |
| self.created = datetime.datetime.now() | |
| self.modified = datetime.datetime.now() | |
| super(Base, self).save(*args, **kwargs) | |
| @property | |
| def is_modified(self): | |
| return self.modified > self.created | |
| tagging.register(Base) | |
| class Person(Base): | |
| # ... | |
| class Meta: | |
| abstract = True | |
| # ... | |
| def full_name(self): | |
| return u'%s %s' % (self.name_first, self.name_last) | |
| class Athlete(Person): | |
| # ... | |
| class Meta: | |
| abstract = True | |
| # ... | |
| class Staff(Person): | |
| # ... | |
| class Meta: | |
| abstract = True | |
| # ... | |
| class Place(Base): | |
| # ... | |
| class Meta: | |
| abstract = True | |
| # ... | |
| def save(self, *args, **kwargs): | |
| # If latlng has no value: | |
| if not self.latlng: | |
| # Add + between fields with values: | |
| location = '+'.join(filter(None, (self.address1, self.address2, self.city, self.state, self.zip, self.country))) | |
| # Attempt to get latitude/longitude from Google Geocoder service v.3: | |
| self.latlng = get_lat_lng(location) | |
| super(Place, self).save(*args, **kwargs) | |
| def get_lat_lng(self): | |
| if self.latlng: | |
| return self.latlng.split(',') | |
| class Event(Base): | |
| # ... | |
| class Meta: | |
| abstract = True | |
| # ... | |
| ###################################### | |
| ## | |
| ## Models: | |
| ## | |
| ###################################### | |
| class Season(Base): | |
| # ... | |
| class Conference(Base): | |
| # ... | |
| class Team(Base): | |
| # ... | |
| class Coach(Staff): | |
| # ... | |
| class Player(Athlete): | |
| # ... | |
| class Schedule(Base): | |
| # ... | |
| class Game(Event): | |
| # ... | |
| class Stadium(Place): | |
| # ... | |
| class Page(Base): | |
| # ... | |
| # ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment