简体   繁体   English

DJANGO模型代理人骚扰

[英]DJANGO models proxy inharitence

I would like to add the methods of the 'tools' class for 2 of my django model calss. 我想为我的django模型calss中的2个添加“工具”类的方法。 Each class will use the same methods with it's own model eample: 每个类将使用相同的方法以及自己的模型样本:

class mapA(models.Model):
     mInd = models.IntegerField()
     scId = models.IntegerField()

class mapB(models.Model):
     mInd = models.IntegerField()
     scId2 = models.IntegerField()

I would like to add the methods like checkInput() to both of them. 我想将像checkInput()这样的方法添加到它们两个中。 So I could run: 所以我可以运行:

mapBInstance.checkInput();
mapAInstance.checkInput();

Ech time the checkInput runs over the data in the mapA or mapB. 检查输入运行在mapA或mapB中的数据的时间。

I thought about creating a tools class & let each model to inherit from it. 我考虑过创建一个工具类,并让每个模型都继承自它。 This way the tools class will have logic which is identical to both maps. 这样,工具类将具有与两个映射相同的逻辑。

When I read the django docs I didn't see example to this case only close solutions. 当我阅读django 文档时,我没有看到这种情况的示例,只是封闭的解决方案。 Is this the correct solution (to use the proxy class)? 这是正确的解决方案(使用代理类)吗?

class Tools():
   def __init__():
      ...init class...
   def checkInput():
       ..make the checks..

class MapA(Tools, models.Model):
     mInd = models.IntegerField()
     scId = models.IntegerField()

     def checkSelf():
         self.checkInput(self.objects.filter(....))

class MapB(Tools, models.Model):
     mIndB = models.IntegerField()
     scIdB = models.IntegerField()
     def checkSelf():
         self.checkInput(self.objects.filter(....))

If you want MapA and MapB (it would be really helpful if you followed PEP-8 ) to be distinct models, proxy models won't help you. 如果你想MapAMapB (如果您是这将是非常有益的PEP-8是完全不同的模型),代理模式不会帮你。 A proxy model is a model that is different in Python, but in the database it is exactly the same as the model it inherits from. 代理模型是在Python中不同的模型,但是在数据库中,它与从其继承的模型完全相同。 Creating a proxy model that doesn't directly inherit from a single concrete model (one that has a table in the database) is an error. 创建不直接从单个具体模型(在数据库中有一个表)继承的代理模型是错误的。

What you're looking for is an abstract base class: 您正在寻找的是一个抽象基类:

class Tools(models.Model):
    ...

    class Meta:
        abstract = True

class MapA(Tools):
    ...

class MapB(Tools):
    ...

An abstract model does not create its own table in the database. 抽象模型没有在数据库中创建自己的表。 Instead, it is as if everything defined in Tools has been defined in both MapA and MapB , but the Tools class is otherwise ignored. 相反,这是因为如果在规定的一切Tools已经在这两个被定义MapAMapB ,但是Tools类,否则忽略。 This allows you to specify all the methods and fields just once, but still have two separate tables in the database. 这样,您就可以一次指定所有方法和字段,但是数据库中仍然有两个单独的表。

A few things... 一些东西...


There is no this in Python, it's called self . Python中没有this ,叫做self


If you're in Python 2.x, tools should inherit from object . 如果您使用的是Python 2.x,则tools应继承自object In Python 3, it's implicit, but doesn't hurt: 在Python 3中,它是隐式的,但不会造成伤害:

class tools(object):
    ...

If you're overriding __init__ in your mixin class ( tools ), then map classes should probably inherit from it first : 如果您在mixin类( tools )中覆盖__init__ ,则map类应该首先从其继承:

class mapA(tools, models.Model):
    ...

Only override __init__ if you really need to, it can get complicated. 仅在确实需要时才重写__init__ ,它会变得很复杂。


Class names are pretty much always in CamelCase. 在CamelCase中,类名几乎总是存在的。 This is not required, but is a convention. 这不是必需的,但这是一个约定。 Also, It's a good idea to name mixin classes transparently: 另外,最好透明地命名混合类:

class ToolsMixin(object):
    ...

class MapA(ToolsMixin, models.Model):
    ...

Other then all that, you perfectly can add a method in a mixin and use it in your models. 除此之外,您可以完美地在mixin中添加方法并在模型中使用它。 No need for Django proxy models. 无需Django代理模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM