我应该在models.queryset子对象上使用不返回queryset的方法吗?

2024-04-20 08:22:53 发布

您现在位置:Python中文网/ 问答频道 /正文

现在在工作中,我遇到了这样的情况,我正试图找到最干净的解决办法:

FooQuerySet(models.QuerySet):

   def _do_something_with_items(items):
       """ some kind of calculation """

   def active_items(self, items):
      new_items = self._do_something_with_items(items)
      return self.filter(active__in=items)

问题是,如果方法_do_something_with_items不返回queryset,而是进行一些数据转换,那么它是否应该留在QuerySet子类中,或者将它留在调用它的位置旁边,这样可以吗


Tags: selfmodelsdefwith情况itemssomedo