Django:抽象类中的自定义模型管理器查询

发布于 2024-12-06 08:10:03 字数 1871 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂牵梦绕锁你心扉 2024-12-13 08:10:03

以下是有关自定义管理器和模型继承的信息: https://docs.djangoproject.com/en/dev/topics/db/managers/#custom-managers-and-model-inheritance

一般问题:通常是否可以为抽象类创建自定义模型管理器并在继承抽象类的模型中使用它们?

答案:是的,

你是对的,但是抽象基类不能被查询。我不太确定你在做什么(选项看起来如何,或者你如何查询它),但希望一般信息会有所帮助

尝试这个例子

class Encyclopedia(Book):
   pass

然后查询:

> Encyclopedia.objects.computePrice(me, some_print_job)

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?

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

class Encyclopedia(Book):
   pass

And then query:

> Encyclopedia.objects.computePrice(me, some_print_job)
如梦 2024-12-13 08:10:03

部分(如果不是全部)问题是 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 : ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文