Django中多个模型的单一自定义管理器

Django中多个模型的单一自定义管理器

本文介绍了Django中多个模型的单一自定义管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个模型通过ForeignKeys关系彼此连接。

这种层次结构中的主要内容包含一个所有者字段。

I have several models connected to each other with ForeignKeys relationships.
The main one in this sort of hierarchy contains a owner field.

我想为所有这些模型创建一个单一自定义管理器,这会更改返回的查询集,具体取决于正在调用它的模型。

I would like to create a single custom manager for all these models that changes the returned queryset depending on the models that is calling it.

我知道经理可以访问 self.model 来获取它附加的模型。

I know that manager can access self.model to get the model that it is attached to.

Class Main(models.Model)
  owner=models.ForeignKey (User)
  owned = OwnedManager()

Class Second(models.Model)
  main=models.ForeignKey('Main')
  owned = OwnedManager()

Class Third(models.Model)
  second=models.ForeignKey('Second')
  owned = OwnedManager()

我希望我的自定义管理器有这样的行为:

I would like my Custom Manager to have this sort of behavior:

class OwnedManager(models.Manager):
    def get_owned_objs(self, owner):
        if self.model == 'Main': # WRONG: How do I get the model name?
            owned_main = self.filter(owner=owner)
            return owned_main
        elif self.model == 'Second':
            owned_second = self.filter(main__owner=owner)
            return owned_second
        else:
            owned_third = self.filter(second__main__owner=owner)
            return owned_third

为了有一个一致的方式在不同的模型中调用它,如下所示:

In order to have a consistent way to call it across different models, like so:

main_object.owned.get_owned_objs(owner=user1) # of the Model Main
second_object.owned.get_owned_objs(owner=user1) # of the Model Second
third_object.owned.get_owned_objs(owner=user1) # of the Model Third

QUESTION:


  • self.model =='Main'是错误的。我不会得到这样的型号。有没有办法得到它?

  • 这是否有效?你知道一个更好的方法来实现吗?也许自定义管理者继承?

  • self.model == 'Main' is wrong. I don't get the model name like this. Is there a way to get it?
  • Is this efficient? Do you know a better way to implement this? Maybe Custom Managers Inheritance?

编辑 - 我的解决方案:
以下接受的答案是一个很好的解决方案,找到一种方法来获取调用自定义管理器的特定模型的模型名称,即:

EDIT - MY SOLUTION:The accepted answer below is a good solution but I also found a way to get the model name of the particular model calling the custom manager, that is:

if self.model.__name__ == 'Main':

这里的关键是属性 __ name __

The key here is the attribute __name__

推荐答案

1)制作抽象模型

class AbstractModel(models.Model):
    class Meta(models.Meta):
        abstract = True

    objects = OwnedManager()

2)从AbstractModel继承您的模型,将一些关键字放在元

2) Inherit your models from AbstractModel, put some key in meta

class Model(AbstractModel)
    class Meta(AbstractModel.Meta):
        filter_key = 'some_key'

3)重新设计您的OwnedManager

3) Redesign your OwnedManager

class OwnedManager(models.Manager):
    def get_owned_objs(self, owner):
        if hasattr(self.model._meta, 'filter_key'):
            return self.filter(**{self.model._meta.filter_key: owner})

现在您可以使用 SomeModel.objects.get_owned_objs(owner = user1)在任何继承的模型中,其中 filter_key 被设置,而没有得到模型的名称。

Now you can use SomeModel.objects.get_owned_objs(owner=user1) in any inherited models, where filter_key is setted without getting models's name.

这篇关于Django中多个模型的单一自定义管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 18:43