【发布时间】:2011-09-26 01:39:47
【问题描述】:
我正在做一个 django 项目,在该项目中我创建了一组三个抽象模型,稍后我将用于各种应用程序,所有这些应用程序都将包含三个模型的层次结构。例如:
class Book(models.Models):
name = models.CharField(...)
author = models.CharField(...)
...
objects = BookManager()
class Meta:
abstract = True
class Page(models.Models):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
chapter = models.CharField(...)
...
objects = PageManager()
class Meta:
abstract = True
class Word(models.Models):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
line = models.IntegerField(...)
...
objects = WordManager()
class Meta:
abstract = True
除了这些抽象类之外,我还想为每个抽象类提供自定义的模型管理器类,以便稍后在继承管理器的应用程序中派上用场
class BookManager(models.Manager):
def computePrice(self, user, printJob):
price = 0
...
class PageManager(models.Manager):
def getPagesOfBook(self, book):
return self.filter(content_type=book, object_id=book.id)
...
....
一般问题:这通常可以为抽象类创建自定义模型管理器并在继承抽象类的模型中使用这些管理器吗?
我问的原因是因为我每次运行查询时都无法在没有收到一组异常漏洞的情况下实现这一点。我还尝试了一种不同的方法,即在抽象视图中运行查询(我还创建了一个抽象视图),尽管发生了相同的异常。
如果我没记错的话,那么这不起作用的部分原因是因为无法查询抽象模型。我要问的基本上是是否有一种方法可以实现我上面描述的内容,如果可以,如何实现。
Request Method: POST
Request URL: http://127.0.0.1:8000/
Django Version: 1.3
Exception Type: AttributeError
Exception Value: 'Options' object has no attribute '_join_cache'
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py in setup_joins, line 1267
Python Executable: /usr/bin/python
Python Version: 2.7.1
【问题讨论】:
标签: django inheritance django-models abstract-class