1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 | 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()
|
Comments
Another place to grab
_review_fieldwould be incontribute_to_class-- that's the method called when the model metaclass adds the manager to the class. Just be sure to call the superclass method!In fact, I'd say that
contribute_to_classwould be the correct place to do this initialization sincecontribute_to_classis the "official" mechanism by which "things attached to models" get to poke at the model.#