我的应用程序需要几个字段所需的属性,所以我去了并遵循代码来创建custom fields。
这是我的CustomCharacterField:
class CustomCharField(models.CharField):
def __int__(self, success_order=None, *args, **kwargs):
self.success_order = success_order
super(CustomCharField, self).__int__( *args, **kwargs)
def get_success_order(self):
return int(self.success_order)
def deconstruct(self):
name, path, args, kwargs = super(CustomCharField, self).deconstruct()
del kwargs["success_order"]
return name, path, args, kwargs
这是我的models.py
class NameModel(models.Model):
name = fields.CustomCharField(max_length=250, unique=True, success_order=1)
这是追溯:
File "/home/kt/Documents/phc/phc/Forms/models.py", line 204, in <module>
class SchemeModel(models.Model):
File "/home/kt/Documents/phc/phc/Forms/models.py", line 220, in SchemeModel
scheme_name = fields.CustomCharField(verbose_name="Scheme", max_length=250, unique=True, success_order=1)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 1072, in __init__
super(CharField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'success_order'
答案 0 :(得分:3)
我认为你这里只有一个拼写错误 - def __int__
应该是def __init__
,super(...).__int__(..)
来电应该是super(...).__init__(..)
。
答案 1 :(得分:-1)
这是因为您传递参数的顺序。回溯显示success_order正被传递给CharField的构造函数,而不应该这样。这是因为它是以kwargs传递的。改变顺序应该可以解决问题。 unique = True将被CharField构造函数接受。