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
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)
Part if not all of the problem is that objects
is not inherited between Django model classes.
It's a "feature" not a bug : ).
精彩评论