Django:抽象类中的自定义模型管理器查询
我正在开发一个 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
I am working on a django project in which I create a set of three abstract models that I will use for a variety of apps later on all of which will contain this hierarchy of three models. E.g.:
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
Besides these abstract classes, I want to provide customized Model Manager classes for each abstract class that will come in handy later on in the apps that inherit the managers
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)
...
....
General question: Is this generally possible to create customized model managers for abstract classes and to use those in the models that inherit the abstract class?
The reason why I am asking is because I have not been able to implement this without receiving a hole set of exceptions every time I would run queries. I also tried a different way by running the queries in the abstract view (I also create an abstract view) though same exceptions occur.
If I am not mistaken, then part of the reason why this is not working is because abstract models cannot be queried. What I am asking is basically if there is a way how I can implement what I described above and if so, how that could be done.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是有关自定义管理器和模型继承的信息: https://docs.djangoproject.com/en/dev/topics/db/managers/#custom-managers-and-model-inheritance
答案:是的,
你是对的,但是抽象基类不能被查询。我不太确定你在做什么(选项看起来如何,或者你如何查询它),但希望一般信息会有所帮助
尝试这个例子
然后查询:
Here is the information on custom managers and model inheritance: https://docs.djangoproject.com/en/dev/topics/db/managers/#custom-managers-and-model-inheritance
Answer: Yes
You are right that abstract base classes can't be queried though. I'm not exactly sure what you're doing (how Options looks, or how you're querying it) but hopefully the general information will help
Try this as an example
And then query:
部分(如果不是全部)问题是 Django 模型类之间没有继承对象。
这是一个“功能”而不是错误:)。
Part if not all of the problem is that
objects
is not inherited between Django model classes.It's a "feature" not a bug : ).