简体   繁体   English

Django模型的子类无法从超类访问

[英]Subclass of Django Model Not Accessible from Superclass

I'm trying to create an inherited model in Django like that below. 我正在尝试在Django中创建一个继承的模型,如下所示。 I should be able to call, for an instance superclass = Superclass(), I should be able to call superclass.subclass, and access the requisite fields. 我应该能够调用,对于一个实例superclass = Superclass(),我应该能够调用superclass.subclass,并访问必需的字段。 When I do that, I'm told that '1 argument was expected, and 8 were given': any idea where I'm going wrong? 当我这样做的时候,我被告知'有一个论点是预期的,而且有8个被给出':任何想法我哪里出错了?

class Superclass(models.Model):
        pass

class Subclass(Superclass):
        def __init__(self):
                super(Subclass, self).__init__()

The problem here is that you need *args and **kwargs in the subclass constructor and superclass constructor. 这里的问题是你需要在子类构造函数和超类构造函数中使用*args**kwargs These two fields will take in the arguments and objects: these include the field information for the superclass, as well as the object manager, among other things. 这两个字段将接受参数和对象:这些字段包括超类的字段信息,以及对象管理器等。 This should do the trick: 这应该做的伎俩:

class Subclass(Superclass):
    def __init__(self, *args, **kwargs):
            super(Subclass, self).__init__(*args, **kwargs)

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

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