Django South:如何为自定义字段创建规则?
我创建了一个自定义字段“Private FileField”。我无法让它与 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 --auto 失败并显示以下消息:
Exception:
(当调用 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”作为“的参数”文件'。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
呸,我试图在评论中写下这个,但缺少段落让我讨厌。
我刚刚在 Django 应用程序中按原样设置了自定义字段,并创建了一个虚拟模型来使用它。南工作完美。我添加了另一个 FileField 并且能够毫无问题地运行
schemamigration --auto
。所以,我很确定你已经正确设置了 South。老实说,您是否检查过您的模型以确保它具有
upload_to
参数?这正是导致此错误消息的原因(这意味着 South 完全按照您的指示做了)。Bah, I tried to write this in a comment, but the lack of paragraphs hates me.
I just set up your custom field as-is in a Django app, and created a dummy model to use it. South works perfectly. I added another FileField and was able to run
schemamigration --auto
with no issues. So, I'm pretty sure you have South set up correctly.Honestly, did you check your model to make sure that it has the
upload_to
parameter? That causes exactly this error message (and it would mean South did exactly what you told it to do).