Django:抽象类中的自定义模型管理器查询 [英] Django: Custom Model Manager queries in abstract classes

查看:134
本文介绍了Django:抽象类中的自定义模型管理器查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个django项目中,在其中创建一组三个抽象模型,以后将用于各种应用程序,所有这些模型将包含三个模型的层次结构。例如:

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

除了这些抽象类,我还想为每个抽象类提供自定义的Model Manager类,这些类将在以后的版本中派上用场。继承管理器的应用

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


推荐答案

以下是有关自定义管理器和模型继承的信息: 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


一般问题:通常可以创建定制模型吗

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?

答案:是

您是正确的,但是不能查询抽象基类。我不确定自己在做什么(选项的外观或查询方式),但希望常规信息会有所帮助

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

示例

class Encyclopedia(Book):
   pass

然后查询:

> Encyclopedia.objects.computePrice(me, some_print_job)

这篇关于Django:抽象类中的自定义模型管理器查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆