1 | from django.db import models
|
---|
2 | from django.contrib.auth.models import User
|
---|
3 |
|
---|
4 |
|
---|
5 | class QuerySetManager(models.Manager):
|
---|
6 | """
|
---|
7 | Simon Willison's QuerySetManager
|
---|
8 | http://djangosnippets.org/snippets/734/
|
---|
9 |
|
---|
10 | Enables adding new QuerySet methods using a Model inner class.
|
---|
11 | """
|
---|
12 |
|
---|
13 | def get_query_set(self):
|
---|
14 | return self.model.QuerySet(self.model)
|
---|
15 |
|
---|
16 | def __getattr__(self, attr, *args):
|
---|
17 | return getattr(self.get_query_set(), attr, *args)
|
---|
18 |
|
---|
19 |
|
---|
20 | class TestModel(models.Model):
|
---|
21 |
|
---|
22 | user = models.ForeignKey(User)
|
---|
23 | string = models.CharField(max_length=64, null=True, blank=True)
|
---|
24 |
|
---|
25 | objects = QuerySetManager()
|
---|
26 |
|
---|
27 | class QuerySet(models.query.QuerySet):
|
---|
28 | pass
|
---|