【问题标题】:Django South: How to create rules for a Custom Field?Django South:如何为自定义字段创建规则?
【发布时间】:2011-03-11 02:11:54
【问题描述】:

我创建了一个自定义字段“Private FileField”。我无法让它与 django-south 一起使用。

我对南场规则的理解是基于 http://south.aeracode.org/docs/tutorial/part4.html#tutorial-part-4http://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


【解决方案1】:

呸,我试图在评论中写这个,但是缺少段落让我讨厌。

我刚刚在 Django 应用程序中按原样设置了您的自定义字段,并创建了一个虚拟模型来使用它。南方完美运行。我添加了另一个 FileField 并且能够毫无问题地运行 schemamigration --auto。所以,我很确定你已经正确设置了 South。

老实说,您是否检查了您的 模型 以确保它具有 upload_to 参数?这正是导致此错误消息的原因(这意味着 South 完全按照您告诉它的操作)。

【讨论】:

  • 嗨卢克,我不再/无法重现这个问题,虽然它一直困扰着我一段时间,一些 grepping 也表明它现在不再是一个问题。非常感谢您帮我解决了麻烦!
  • 哈!毕竟我并没有发疯……我只是在 South 0.7 重新体验了这一点;但问题在 0.7.3 中得到修复。再次感谢卢克在这里帮助我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 2020-11-18
  • 2017-11-26
  • 2021-02-06
相关资源
最近更新 更多