如何将自定义管理器与相关对象一起使用?
我有一个定制经理。我想将它用于相关对象。我在文档中找到了 use_for_lated_fields 。但它不像我使用它的方式工作:
class RandomQueryset(models.query.QuerySet):
def randomize(self):
count = self.count()
random_index = random.randint(0, count - 1)
return self.all()[random_index]
class RandomManager(models.Manager):
use_for_related_fields = True
def get_query_set(self):
return RandomQueryset(self.model, using=self._db)
def randomize(self):
return self.get_query_set().randomize()
我将它用于一个模型:
>>> post = PostPages.default_manager.filter(image_gallery__isnull=False).distinct().randomize()
并尝试对 m2m 相关对象执行相同的操作:
>>> post.image_gallery.randomize()
遇到错误:
AttributeError: 'ManyRelatedManager' object has no attribute 'randomize'
是否可以按照我的方式使用自定义管理器?如果是这样,你如何让它发挥作用?
编辑
我的模型:
class ShivaImage(models.Model, ImageResizing):
image = models.ImageField(upload_to='img')
slide_show = models.BooleanField()
title = models.CharField(max_length=100)
text = models.TextField(max_length=400)
ordering = models.IntegerField(blank=True, null=True)
objects = RandomManager()
class PostPages(models.Model):
image_gallery = models.ManyToManyField(ShivaImage, blank=True,
related_name='gallery',)
# all the other fields...
objects = RandomManager()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了主题的完整性,Django 1.7(最终)支持 使用自定义反向管理器,这样你就可以做类似的事情(只需从 django 文档复制):
For completeness of the topic, Django 1.7 (finally) supports using a custom reverse manager, so you can do something like that (just copying from the django docs):
仅建议 Django 1.09 或更早版本 - 文档证明
设置管理器上的
use_for_lated_fields
为True
将使其在指向您将此管理器定义为默认管理器的模型的所有关系上可用。这是记录在这里我想你已经有了它仅在您的
PostPages
模型上启用,而不是在您的Gallery
模型(或通过post_image_gallery
引用的任何模型名称)上启用。如果您想在此领域管理器上拥有额外的功能,您需要将use_for_lated_fields = True
添加到您的Gallery
模型中!THIS IS ONLY SUGGESTED FOR Django 1.09 or older - Docs proof
Setting
use_for_related_fields
toTrue
on the manager will make it available on all relations that point to the model on which you defined this manager as the default manager. This is documented hereI suppose you have it only enabled on your
PostPages
model, not on yourGallery
model (or whatever the model is called that is referenced throughpost_image_gallery
). If you want to have additionally functionality on this realtion manager you need to add a custom default manager withuse_for_related_fields = True
to yourGallery
model!在 django 2.0 中
use_for_lated_fields
已弃用 https://docs.djangoproject.com/en/2.0/releases/1.10/#manager-use-for-lated-fields-and-inheritance-changes您应该使用
基本经理名称
:https://docs.djangoproject.com/ en/2.0/ref/models/options/#django.db.models.Options.base_manager_name更新的文档:https://docs.djangoproject。 com/en/2.0/topics/db/managers/#using-managers-for-lated-object-access
In django 2.0
use_for_related_fields
is deprecated https://docs.djangoproject.com/en/2.0/releases/1.10/#manager-use-for-related-fields-and-inheritance-changesYou should use
base_manager_name
: https://docs.djangoproject.com/en/2.0/ref/models/options/#django.db.models.Options.base_manager_nameUpdated docs: https://docs.djangoproject.com/en/2.0/topics/db/managers/#using-managers-for-related-object-access
另外,在自定义管理器中,确保通过 self.get_query_set() 访问查询集
实现从相关管理器调用的自定义过滤器时的代理方法:
示例用法:
Also, in the custom manager, make sure to access the queryset via the self.get_query_set()
proxy method when implementing custom filters to be called from related manager:
Sample usage:
对于 Django 4 和 5,它有点不同:
然后你可以像这样使用它:
请参阅 django 文档
For Django 4 and 5 it's a bit different:
and then you can use it like this:
See django documentation