Django模型方法错误:缺少1个必需的位置参数:'self'

时间:2019-12-10 00:39:43

标签: python django django-models

我想在模型内部以及属性之间进行一些计算。我希望用户可以输入3个字段来添加模型,但是这些字段被串联到一个不可编辑的字段中,该字段只能用于查看目的。

这是我的模特:

class Dossier(models.Model): #Dossiers sinistre
numero = models.CharField(max_length=20, default=dossier_number, editable=False)
created_by = models.ForeignKey(Prestataire, on_delete=models.SET_NULL, null=True)
mat_id = models.CharField(max_length=7, default="123456")
mattypes = (
    ('A', 'A'),
    ('B', 'B'),
    ('C', 'C'),
    ('D', 'D'),
)
mat_symbol = models.CharField(max_length=3, choices=mattypes, default="A")
mat_ville = models.CharField(max_length=2, blank = True)

def define_matricule(self):
    if self.mat_symbol == "A" or "B":
        return str(self.mat_id) + '-' + str(self.mat_symbol) + '-' + str(self.mat_ville)
    else:
        return str(self.mat_id) + '-' + str(self.mat_symbol)

matricule = models.CharField(max_length=20, default=define_matricule, editable=False)

但是当我运行它时,我发现了这个错误:

Django Version:     2.2.5
Exception Type:     TypeError
Exception Value:    

define_matricule() missing 1 required positional argument: 'self'

Exception Location:     C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\__init__.py in get_default, line 797
Python Executable:  C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\python.exe

这是我的追踪记录:

Environment:


Request Method: GET
Request URL: http://localhost:8000/dossiers/nouveau

Django Version: 2.2.5
Python Version: 3.7.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'formtools',
 'dashboard.apps.DashboardConfig',
 'crispy_forms',
 'photologue',
 'sortedm2m']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)

File "D:\verautov0\verautonoapi\dashboard\views.py" in nouveaudossier
  451.     NewForm()

File "D:\verautov0\verautonoapi\dashboard\forms.py" in __init__
  173.         super().__init__(*args, **kwargs)

File "C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\models.py" in __init__
  288.             self.instance = opts.model()

File "C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\base.py" in __init__
  475.                 val = field.get_default()

File "C:\Users\Kaiss Bouali\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\__init__.py" in get_default
  797.         return self._get_default()

Exception Type: TypeError at /dossiers/nouveau
Exception Value: define_matricule() missing 1 required positional argument: 'self'

1 个答案:

答案 0 :(得分:0)

覆盖保存方法以计算依赖于其他字段的字段的示例:

def save(self, **kwargs):
    if self.mat_symbol == "A" or "B":
        self.numero = str(self.mat_id) + '-' + str(self.mat_symbol) + '-' + str(self.mat_ville)
    else:
        self.numero = str(self.mat_id) + '-' + str(self.mat_symbol)
    super(Dossier, self).save(**kwargs)