Manager introspecting attached model

 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()

More like this

  1. Allow filtering and ordering by counts of related query results by exogen 6 years, 1 month ago
  2. Manager method for limiting GenericForeignKey queries by zerok 4 years, 9 months ago
  3. improved generic foreign key manager by carljm 4 years, 8 months ago
  4. Improved generic foreign key manager 2 by Nomalz 3 years, 6 months ago
  5. Querying on existence of a relationship by ubernostrum 5 years, 9 months ago

Comments

jacobian (on February 25, 2008):

Another place to grab _review_field would be in contribute_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_class would be the correct place to do this initialization since contribute_to_class is the "official" mechanism by which "things attached to models" get to poke at the model.

#

(Forgotten your password?)