Django South:如何为自定义字段创建规则?

发布于 2024-09-08 03:43:25 字数 2372 浏览 7 评论 0 原文

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

我对南田规则的理解是基于 http://south.aeracode.org/docs/tutorial/ part4.html#tutorial-part-4http://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: 需要一个关键字 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”作为“的参数”文件'。

I've create a custom field "Private FileField". I'm not able to get it to work with django-south.

My understanding of South field rules is based on
http://south.aeracode.org/docs/tutorial/part4.html#tutorial-part-4 and
http://south.aeracode.org/docs/customfields.html

Relevant snippets are:

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)

and

rules = [
  (
    (FileField,),
    [],
    {
        "upload_to": ["upload_to", {}],
    },
  )
]

from south.modelsinspector import add_introspection_rules
add_introspection_rules(rules, ["^private_filefield\."])

Running a manage.py schemamigration my_app_name --auto fails with the following message:

Exception: <class 'private_filefield.fields.FileField'> expects one keyword arg 'upload_to'

(this happes when site-packages/south/orm.py", line 46, in FakeORM is called)

Full code can be found on:
http://bitbucket.org/vanschelven/django_private_filefield/src/tip/private_filefield/fields.py

=== Edit: text below added ===

This is the relevant section of the generated 'models' section of the auto-generated migration:

    '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'})
    }

Note the lack of 'upload_to' as paramter to 'file'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2024-09-15 03:43:26

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

我刚刚在 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文