我正试图找到一种方法来实现自定义QuerySet
和自定义Manager
而不会破坏DRY。这就是我到目前为止所做的:
class MyInquiryManager(models.Manager):
def for_user(self, user):
return self.get_query_set().filter(
Q(assigned_to_user=user) |
Q(assigned_to_group__in=user.groups.all())
)
class Inquiry(models.Model):
ts = models.DateTimeField(auto_now_add=True)
status = models.ForeignKey(InquiryStatus)
assigned_to_user = models.ForeignKey(User, blank=True, null=True)
assigned_to_group = models.ForeignKey(Group, blank=True, null=True)
objects = MyInquiryManager()
这样做很好,直到我这样做:
inquiries = Inquiry.objects.filter(status=some_status)
my_inquiry_count = inquiries.for_user(request.user).count()
这会立即中断所有内容,因为QuerySet
与Manager
的方法不同。我尝试创建自定义QuerySet
类,并在MyInquiryManager
中实现它,但最终我复制了所有方法定义。
我还发现this snippet有效,但我需要将额外的参数传递给for_user
,因此它会因为重新定义get_query_set
而严重依赖。
如果没有在QuerySet
和Manager
子类中重新定义所有方法,有没有办法做到这一点?
答案 0 :(得分:49)
Django已经改变了!在使用2009年编写的这个答案中的代码之前,请务必查看其余的答案和Django文档,看看是否有更合适的答案。溶液
我实现此方法的方法是将实际的get_active_for_account
添加为自定义QuerySet
的方法。然后,为了让它在管理器上工作,您可以简单地捕获__getattr__
并相应地返回
为了使这个模式可重用,我将Manager
位提取出来给一个单独的模型管理器:
<强> custom_queryset / models.py 强>
from django.db import models
from django.db.models.query import QuerySet
class CustomQuerySetManager(models.Manager):
"""A re-usable Manager to access a custom QuerySet"""
def __getattr__(self, attr, *args):
try:
return getattr(self.__class__, attr, *args)
except AttributeError:
# don't delegate internal methods to the queryset
if attr.startswith('__') and attr.endswith('__'):
raise
return getattr(self.get_query_set(), attr, *args)
def get_query_set(self):
return self.model.QuerySet(self.model, using=self._db)
一旦你有了这个,你需要做的就是将QuerySet
定义为自定义内部类,并将管理器设置为自定义管理器:
<强> your_app / models.py 强>
from custom_queryset.models import CustomQuerySetManager
from django.db.models.query import QuerySet
class Inquiry(models.Model):
objects = CustomQuerySetManager()
class QuerySet(QuerySet):
def active_for_account(self, account, *args, **kwargs):
return self.filter(account=account, deleted=False, *args, **kwargs)
使用这种模式,其中任何一种都可以起作用:
>>> Inquiry.objects.active_for_account(user)
>>> Inquiry.objects.all().active_for_account(user)
>>> Inquiry.objects.filter(first_name='John').active_for_account(user)
UPD如果您将其与自定义用户(AbstractUser
)一起使用,则需要更改
来自
class CustomQuerySetManager(models.Manager):
到
from django.contrib.auth.models import UserManager
class CustomQuerySetManager(UserManager):
***
答案 1 :(得分:29)
Django 1.7发布了一种新的简单方式来创建组合查询集和模型管理器:
class InquiryQuerySet(models.QuerySet):
def for_user(self):
return self.filter(
Q(assigned_to_user=user) |
Q(assigned_to_group__in=user.groups.all())
)
class Inquiry(models.Model):
objects = InqueryQuerySet.as_manager()
有关详细信息,请参阅Creating Manager with QuerySet methods。
答案 2 :(得分:10)
您可以使用mixin在manager和queryset上提供方法。请参阅以下技术:
http://hunterford.me/django-custom-model-manager-chaining/
这也避免了使用__getattr__()
方法。
from django.db.models.query import QuerySet
class PostMixin(object):
def by_author(self, user):
return self.filter(user=user)
def published(self):
return self.filter(published__lte=datetime.now())
class PostQuerySet(QuerySet, PostMixin):
pass
class PostManager(models.Manager, PostMixin):
def get_query_set(self):
return PostQuerySet(self.model, using=self._db)
答案 3 :(得分:3)
T. Stone的方法略有改进版本:
def objects_extra(mixin_class):
class MixinManager(models.Manager, mixin_class):
class MixinQuerySet(QuerySet, mixin_class):
pass
def get_query_set(self):
return self.MixinQuerySet(self.model, using=self._db)
return MixinManager()
类装饰器的用法简单如下:
class SomeModel(models.Model):
...
@objects_extra
class objects:
def filter_by_something_complex(self, whatever parameters):
return self.extra(...)
...
更新:支持非标准的Manager和QuerySet基类,例如: G。 @objects_extra(django.contrib.gis.db.models.GeoManager,django.contrib.gis.db.models.query.GeoQuerySet):
def objects_extra(Manager=django.db.models.Manager, QuerySet=django.db.models.query.QuerySet):
def oe_inner(Mixin, Manager=django.db.models.Manager, QuerySet=django.db.models.query.QuerySet):
class MixinManager(Manager, Mixin):
class MixinQuerySet(QuerySet, Mixin):
pass
def get_query_set(self):
return self.MixinQuerySet(self.model, using=self._db)
return MixinManager()
if issubclass(Manager, django.db.models.Manager):
return lambda Mixin: oe_inner(Mixin, Manager, QuerySet)
else:
return oe_inner(Mixin=Manager)
答案 4 :(得分:0)
您现在可以在管理器上使用from_queryset()方法来更改其基本查询集。
这允许您仅定义一次Queryset方法和管理器方法
来自文档
对于高级用法,您可能需要自定义管理器和自定义QuerySet。您可以通过调用Manager.from_queryset()来实现,该方法返回基本Manager的子类以及自定义QuerySet方法的副本:
class InqueryQueryset(models.Queryset):
def custom_method(self):
""" available on all default querysets"""
class BaseMyInquiryManager(models.Manager):
def for_user(self, user):
return self.get_query_set().filter(
Q(assigned_to_user=user) |
Q(assigned_to_group__in=user.groups.all())
)
MyInquiryManager = BaseInquiryManager.from_queryset(InquiryQueryset)
class Inquiry(models.Model):
ts = models.DateTimeField(auto_now_add=True)
status = models.ForeignKey(InquiryStatus)
assigned_to_user = models.ForeignKey(User, blank=True, null=True)
assigned_to_group = models.ForeignKey(Group, blank=True, null=True)
objects = MyInquiryManager()
答案 5 :(得分:0)
在某些用例中,我们需要 call custom QuerySet methods from the manager 而不是使用 QuerySet 的 "dt":f"INSERT INTO mark_helper_rollup (id, on_date, site_id, dt_calls, added_on) VALUES ('{dbdata[0]}','{dbdata[1]}',{dbdata[2]},{dbdata[3]},'{dbdata[4]}') ON DUPLICATE KEY UPDATE dt_Calls={dbdata[3]}, added_on='{dbdata[4]}';",
"ub":f"INSERT INTO mark_helper_rollup (id, on_date, site_id, ub, added_on) VALUES ('{dbdata[0]}','{dbdata[1]}',{dbdata[2]},{dbdata[3]},'{dbdata[4]}') ON DUPLICATE KEY UPDATE ub={dbdata[3]}, added_on='{dbdata[4]}';",
"wp":f"INSERT INTO mark_helper_rollup (id, on_date, site_id, wp, added_on) VALUES ('{dbdata[0]}','{dbdata[1]}',{dbdata[2]},{dbdata[3]},'{dbdata[4]}') ON DUPLICATE KEY UPDATE wp={dbdata[3]}, added_on='{dbdata[4]}';",
"sn":f"INSERT INTO mark_helper_rollup (id, on_date, site_id, sn, added_on) VALUES ('{dbdata[0]}','{dbdata[1]}',{dbdata[2]},{dbdata[3]},'{dbdata[4]}') ON DUPLICATE KEY UPDATE sn={dbdata[3]}, added_on='{dbdata[4]}';",
"ga":f"INSERT INTO mark_helper_rollup (id, on_date, site_id, ga_organic, added_on) VALUES ('{dbdata[0]}','{dbdata[1]}',{dbdata[2]},{dbdata[3]},'{dbdata[4]}') ON DUPLICATE KEY UPDATE ga_organic={dbdata[3]}, added_on='{dbdata[4]}';",
方法。
根据已接受的解决方案评论中发布的解决方案,mixin 就足够了。
get_manager
例如
class CustomQuerySetManagerMixin:
"""
Allow Manager which uses custom queryset to access queryset methods directly.
"""
def __getattr__(self, name):
# don't delegate internal methods to queryset
# NOTE: without this, Manager._copy_to_model will end up calling
# __getstate__ on the *queryset* which causes the qs (as `all()`)
# to evaluate itself as if it was being pickled (`len(self)`)
if name.startswith('__'):
raise AttributeError
return getattr(self.get_queryset(), name)
有了上面的内容,我们可以像下面这样访问相关的对象(Book),而无需在管理器中为每个查询集方法定义新方法。
class BookQuerySet(models.QuerySet):
def published(self):
return self.filter(published=True)
def fiction(self):
return self.filter(genre="fiction")
def non_fiction(self):
return self.filter(genre="non-fiction")
class BookManager(CustomQuerySetManagerMixin, models.Manager):
def get_queryset(self):
return BookQuerySet(self.model, using=self._db).published()
class Book(models.Model):
title = models.CharField(max_length=200)
genre = models.CharField(choices=[('fiction', _('Fiction')), ('non-fiction', _('Non-Fiction'))])
published = models.BooleanField(default=False)
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name="books")
objects = BookManager()
class Author(models.Model):
name = models.CharField(max_length=200)
答案 6 :(得分:-1)
以下适用于我。
def get_active_for_account(self,account,*args,**kwargs):
"""Returns a queryset that is
Not deleted
For the specified account
"""
return self.filter(account = account,deleted=False,*args,**kwargs)
这是在默认管理器上;所以我曾经做过类似的事情:
Model.objects.get_active_for_account(account).filter()
但是没有理由不应该为二级经理工作。