澄清/何时/何地在Django/Python中使用super [英] clarification/when/where to use super in django/python

查看:263
本文介绍了澄清/何时/何地在Django/Python中使用super的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我们知道super用于查找父类"并返回其对象,类似于/使用self.__class__.__mro__

我感到困惑的是何时准确使用它?

假设我们有一个如下的示例模型代码

版本_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

版本_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

因此我们可以在上面看到

version_1 中,我首先调用了super并对字段进行了修改,并返回了obj

version_2 中,我修改了字段,然后调用了super并返回了obj

那么在修改字段前后调用super会发生什么?

最后我想知道/确认的是

  • 在Django表单/模型中何处/为何确切使用super.
  • 在django/python中使用它们的确切概念是什么(如果我理解这是错误的话).

解决方案

您的Example类是Model的子类.这意味着Example继承了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方法.

   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()实现.

   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 上查看模型定义. /p>

如果您感到兴奋,可以通过通话

深入到django ORM.

Actually we know super is used to find the "parent class" and return its object, something like/using self.__class__.__mro__

What I was confused about is when to use it exactly?

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

In version_1 I have called super first and done modifications to fields and returned the obj

In version_2 I had modified the fields and after that called super and returned obj

So what happens when super is called before and after modification of fields?

Finally what i want to know/confirm is

  • Where/why exactly super is used in django forms/models.
  • What is the exact concept of using them in django / python (if I understood this wrong).

解决方案

Your Example class is a subclass of Model. That means, that Example inherits all functions from Model.

Look at the following:

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'

Now with inheritance.

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"

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"

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆