【问题标题】:Constructor issues with django models multiple inheritancedjango 模型多重继承的构造函数问题
【发布时间】:2013-04-10 21:52:21
【问题描述】:

在我的 django 应用程序的 models.py 中,我从两个类继承:models.Model 和一个名为 Isbn10 的类,它来自导入的 Python 模块 pyisbn。但是,如果我尝试创建该类的实例,则会收到 TypeError: __init__() 恰好需要 2 个参数(给定 1 个)

我试过颠倒父类的顺序,但没有帮助。

型号:

from django.db import models
import pyisbn

class Book10(pyisbn.Isbn10, models.Model):
    pass

Isbn 类定义(在 pyisbn 模块中定义):

class Isbn(object):
     def __init__(self, isbn):
        super(Isbn, self).__init__()
        self._isbn = isbn
        if len(isbn) in (9, 12):
            self.isbn = _isbn_cleanse(isbn, False)
        else:
            self.isbn = _isbn_cleanse(isbn)

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    您的 init() 定义需要 self 和 Isbn。所以你必须通过两者。此外,self 应该始终是第一个参数。

    【讨论】:

    • 你的意思是我需要 Isbn(self, '9780887276309') 来创建一个新实例吗?它不起作用。请注意,Isbn 类是在 pyisbn 中定义的,pyisbn 是第三方 python 模块,并非特定于 django。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多