from django.db import models class Review(models.Model): LOW_RATING = 1 AVERAGE_RATING = 2 HIGH_RATING = 3 RATING_CHOICES = ( (LOW_RATING, 'Low'), (AVERAGE_RATING, 'Average'), (HIGH_RATING, 'High'), ) title = models.CharField(max_length=250) pub_date = models.DateTimeField() body = models.TextField() rating = models.IntegerField(choices=RATING_CHOICES) class ReviewedObjectManager(models.Manager): def _get_review_field(self): if not hasattr(self, '_review_field'): for f in self.model._meta.fields: if f.rel and f.rel.to == Review: setattr(self, '_review_field', f.name) break return self._review_field def rating_equals(self, rating): review_field = self._get_review_field() return self.filter(**{ '%s__rating' % review_field: rating }) class Movie(models.Model): title = models.CharField(max_length=255) description = models.TextField() running_time = models.PositiveIntegerField() movie_review = models.ForeignKey(Review) objects = ReviewedObjectManager() class Restaurant(models.Model): name = models.CharField(max_length=255) address = models.CharField(max_length=255) description = models.TextField() restaurant_review = models.ForeignKey(Review) objects = ReviewedObjectManager()