回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我的应用程序几乎不需要字段所需的属性,因此我按照代码创建了<a href="https://docs.djangoproject.com/en/1.10/howto/custom-model-fields/" rel="nofollow">custom fields</a>。你知道吗</p>
<p>这是我的CustomCharacterField:</p>
<pre><code>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
</code></pre>
<p>这是我的型号.py你知道吗</p>
<pre><code>class NameModel(models.Model):
name = fields.CustomCharField(max_length=250, unique=True, success_order=1)
</code></pre>
<p>以下是回溯:</p>
<pre><code> 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'
</code></pre>