I'm trying to set up a proxy model in django admin. It will represent a subset of the original model. The code from mode开发者_运维百科ls.py:
class MyManager(models.Manager):
def get_query_set(self):
return super(MyManager, self).get_query_set().filter(some_column='value')
class MyModel(OrigModel):
objects = MyManager()
class Meta:
proxy = True
Now instead of filter() I need to use a complex SELECT statement with JOINS. What's the proper way to inject it wholly to the custom manager?
Django provides the extra() QuerySet modifier -- a hook for injecting specific clauses into the SQL generated by a QuerySet.
This can be used in complex cases, maybe with one or more additional queries.
If you want to use the ORM further in MyModel.objects raw SQL is no solution. In the case of raw SQL an iterator is provided.
You are not able to do any chaining on MyModel().objects as filter, exclude etc.. If it is possible in admin, so for example filtering would not work in it. If you need this features, the only choice you have is not to use raw sql in your managers get_query_set method.
I do not know if Manager.raw is even possible in admin.
精彩评论