【发布时间】:2011-03-11 02:11:54
【问题描述】:
我创建了一个自定义字段“Private FileField”。我无法让它与 django-south 一起使用。
我对南场规则的理解是基于 http://south.aeracode.org/docs/tutorial/part4.html#tutorial-part-4 和 http://south.aeracode.org/docs/customfields.html
相关的sn-ps有:
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 --auto 失败并显示以下消息:
Exception: <class 'private_filefield.fields.FileField'> expects one keyword arg 'upload_to'
(当调用 FakeORM 中的 site-packages/south/orm.py”,第 46 行时,会发生这种情况)
完整代码见: 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 参数。
-
模型的 FileField 确实有 upload_to 参数。但是迁移的自动生成的“模型”部分没有这个参数。所以我想我的问题可以简化为“我如何确保自动生成的模型也包含这个 'upload_to' 参数”?
标签: django django-south