向Django添加简单的自定义字段 - 如何写南方内省规则 [英] Adding Simple Custom Field to Django -- How to Write South Introspection Rules
问题描述
我正在尝试添加一个使用 South 的Django项目的自定义字段。因此,我正在尝试(首次)撰写南方内省规则。我相信我的情况是最简单的,因为我只是扩展一个CharField。具体来说:
class ColorField(models.CharField):
def __init __(self,* args,** kwargs) :
kwargs ['max_length'] = 10
super(ColorField,self).__ init __(* args,** kwargs)
def formfield(self,** kwargs) :
kwargs ['widget'] = ColorPickerWidget
return super(ColorField,self).formfield(** kwargs)
这是一个Django片段,名为 jQuery颜色选择器模型字段对于那些感兴趣的人来说,
由于我没有添加任何新的属性,我相信我只需要添加以下代码行:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([],[^ myproject\.myapp\.models\ColorField])
很明显,但他们应该去哪里?此外,我的假设是,这是我必须做的正确吗?
我已经审查了这里发布的几个问题,但大多数处理更复杂的内省。 >
Per http://south.readthedocs.org/en/latest/customfields.html#where-to-put-the-code ,我已经尝试将代码放在我的models.py文件的顶部,其中自定义字段被定义。但是这没有起作用。
简单的回答:是的,代码应该放在models.py文件中,定义。正确的代码是:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([],[^ myapp\ .models\.ColorField])
不知道为什么我把项目名称放在那里。
I am trying to add a custom field to my Django project that uses South. Because of this, I am trying (for the first time) to write introspection rules for South. I believe my case is the simplest possible as I am simply extending a CharField. Specifically:
class ColorField(models.CharField):
def __init__(self, *args, **kwargs):
kwargs['max_length'] = 10
super(ColorField, self).__init__(*args, **kwargs)
def formfield(self, **kwargs):
kwargs['widget'] = ColorPickerWidget
return super(ColorField, self).formfield(**kwargs)
This is from a Django snippet called jQuery color picker model field for those interested.
Since I am not adding any new attributes, I believe I only have to add these lines of code:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^myproject\.myapp\.models\.ColorField"])
It is probably obvious, but where should they go? Also, is my assumption that this is all I have to do correct?
I have reviewed several questions posted here, but most deal with much more complex introspections.
Per http://south.readthedocs.org/en/latest/customfields.html#where-to-put-the-code, I have tried puttin the code at the top of my models.py file where the custom field is defined. But this has not worked.
Simple answer: yes, the code should go in the models.py file where the field was defined. The correct code is:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^myapp\.models\.ColorField"])
Not sure why I was putting the project name in there.
这篇关于向Django添加简单的自定义字段 - 如何写南方内省规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!