澄清/何时/何地在 django/python 中使用 super [英] clarification/when/where to use super in django/python
问题描述
实际上我们知道 super
用于查找父类"并返回其对象,类似于/使用 self.__class__.__mro__
Actually we know super
is used to find the "parent class" and return its object, something like/using self.__class__.__mro__
让我感到困惑的是什么时候使用它?
假设我们有一个示例模型代码如下
Suppose we have a Example model code as below
version_1
class Example(models.Model):
name = models.CharField()
age = models.IntegerField()
def save(self, **args, **kwargs):
obj = super(Example, self).save(self, **args, **kwargs)
obj.name = "name changed"
obj.age = "age changed"
return obj
version_2
class Example(models.Model):
name = models.CharField()
age = models.IntegerField()
def save(self, **args, **kwargs):
self.name = "name changed"
self.age = "age changed"
obj = super(Example, self).save(self, **args, **kwargs)
return obj
所以我们可以在上面观察
so as we can observe above
在version_1中,我首先调用了super
并对字段进行了修改并返回了obj
In version_1 I have called super
first and done modifications to fields and returned the obj
在 version_2 中,我修改了字段,然后调用了 super 并返回了 obj
In version_2 I had modified the fields and after that called super and returned obj
那么在修改字段之前和之后调用super会发生什么?
So what happens when super is called before and after modification of fields?
最后我想知道/确认的是
Finally what i want to know/confirm is
- 在 Django 表单/模型中究竟在哪里/为什么使用 super.
- 在 django/python 中使用它们的确切概念是什么(如果我理解错了).
推荐答案
您的 Example
类是 Model
的子类.这意味着,Example
继承了 Model
的所有函数.
Your Example
class is a subclass of Model
. That means, that Example
inherits all functions from Model
.
请看以下内容:
class Example():
pass
example = Example()
#let's try to save it
example.save()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Example instance has no attribute 'save'
现在有了继承.
class Parent():
def save(*args, **kwargs):
print "saving now"
class Example(Parent):
pass
example = Example()
#since Example has no save method, it inherits the Parent's save method.
example.save()
"saving now"
如果在 Example 类中覆盖 save() 方法,则不会调用 Parent 的 save 方法.
If you override the save() method in your Example class, the Parent's save method will not be called.
class Example(Parent):
def save(self, *args, **kwargs):
print "i know how to save on my own now"
example = Example()
example.save()
"i know how to save on my own now"
如果你选择调用 super,你就是在调用 Parent 的 save 函数以及你自己的 save() 实现.
If you choose to call super, you are calling the Parent's save function together with your own implementation of save().
class Example(Parent):
def save(self, *args, **kwargs):
super(Example, self).save(*args, **kwargs)
print "i know how to save on my own now"
example = Example()
example.save()
"saving now" #from parent
"i know how to save on my own" #from example
这同样适用于您继承的所有 djangos 类.实际上,实现要复杂得多.您可以在 github 上查看模型定义.
The same applies to all of djangos classes you inherit from. In fact, the implementation is more complex. You can take a look at the Model definition here on github.
如果您感到兴奋,可以通过这个谈话
If you feel thrilled, you can dive into the django ORM with this talk
这篇关于澄清/何时/何地在 django/python 中使用 super的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!