我已經創建了一個自定義字段「Private FileField」。我無法使它與django-south一起工作。Django South:如何爲自定義字段創建規則?
我的南田規則的理解是基於 http://south.aeracode.org/docs/tutorial/part4.html#tutorial-part-4和 http://south.aeracode.org/docs/customfields.html
相關片段是:
class FileField(models.CharField):
__metaclass__ = models.SubfieldBase
def __init__(self, *args, **kwargs):
if not 'upload_to' in kwargs:
raise Exception("%s expects one keyword arg 'upload_to'" % (self.__class__))
self.upload_to = kwargs['upload_to']
del kwargs['upload_to']
kwargs['max_length'] = 255
super(FileField, self).__init__(*args, **kwargs)
和
rules = [
(
(FileField,),
[],
{
"upload_to": ["upload_to", {}],
},
)
]
from south.modelsinspector import add_introspection_rules
add_introspection_rules(rules, ["^private_filefield\."])
運行manage.py schemamigration my_app_name - 自動失敗並顯示以下消息:
Exception: <class 'private_filefield.fields.FileField'> expects one keyword arg 'upload_to'
(此happes當站點包/南/ orm.py」 46行,在FakeORM稱)
的完整代碼上可以找到: http://bitbucket.org/vanschelven/django_private_filefield/src/tip/private_filefield/fields.py
== =編輯:添加下面的文本===
這是自動生成的遷移生成的「模型」部分的相關部分:
'mailfile.mailfile': {
'Meta': {'object_name': 'MailFile'},
'creation_date': ('django.db.models.fields.DateField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'expires_on': ('django.db.models.fields.DateField', [], {'default': 'datetime.date(2010, 7, 16)'}),
'file': ('private_filefield.fields.FileField', [], {'max_length': '255'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
'secret': ('django.db.models.fields.CharField', [], {'max_length': '40'})
}
請注意缺少'upload_to'作爲'file'的參數。
我們可以使用FileField看到模型嗎?從錯誤消息看來,它似乎沒有upload_to參數。 – 2010-07-02 17:02:37
模型的FileField具有upload_to參數。但是,自動生成的「模型」部分遷移沒有此參數。所以我想我的問題可以簡化爲「我如何確保自動生成的模型也包含此'upload_to'參數」? – 2010-07-06 10:29:56