Django-South 反射规则失效

13 投票
1 回答
4225 浏览
提问于 2025-04-16 10:07

我正在使用 Django 1.2.3South 0.7.3

我想把我的应用(叫做 core)转换为使用 Django-South。我有一个自定义的模型/字段,叫做 ImageWithThumbsField。它基本上就是老版的 django.db.models.ImageField,加了一些属性,比如高度、宽度等等。

在尝试运行 ./manage.py convert_to_auth core 时,我遇到了 South 的 冻结 错误。我不知道为什么,可能是我漏掉了什么...

我使用了一个简单的自定义模型:

from django.db.models import ImageField

class ImageWithThumbsField(ImageField):
    def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, sizes=None, **kwargs):
        self.verbose_name=verbose_name
        self.name=name
        self.width_field=width_field
        self.height_field=height_field
        self.sizes = sizes
        super(ImageField, self).__init__(**kwargs)

这是我添加到 models.py 顶部的检查规则:

from south.modelsinspector import add_introspection_rules
from lib.thumbs import ImageWithThumbsField

add_introspection_rules(
    [
        (
            (ImageWithThumbsField, ),
            [],
            {
                "verbose_name": ["verbose_name", {"default": None}],
                "name":         ["name",         {"default": None}],
                "width_field":  ["width_field",  {"default": None}],
                "height_field": ["height_field", {"default": None}],
                "sizes":        ["sizes",        {"default": None}],
            },
        ),
    ],
    ["^core/.fields/.ImageWithThumbsField",])

这是我收到的错误信息:

! Cannot freeze field 'core.additionalmaterialphoto.photo'
! (this field has class lib.thumbs.ImageWithThumbsField)
! Cannot freeze field 'core.material.photo'
! (this field has class lib.thumbs.ImageWithThumbsField)
! Cannot freeze field 'core.material.formulaimage'
! (this field has class lib.thumbs.ImageWithThumbsField)

! South cannot introspect some fields; this is probably because they are custom
! fields. If they worked in 0.6 or below, this is because we have removed the
! models parser (it often broke things).
! To fix this, read http://south.aeracode.org/wiki/MyFieldsDontWork

有没有人知道这是为什么?我哪里做错了?

1 个回答

17

我明白了! :)

我把这个改成了: ["^lib\.thumbs\.ImageWithThumbsField",]

这一整行其实是一个 正则表达式,用来匹配 Django 字段类型 的 python 路径(再读一遍,这句话有点长)。

South 遇到了一个字段名 ImageWithThumbsField,它是在路径 lib.thumbs 中声明的。我给了它一个错误的路径,所以 South 还是不知道该怎么处理这个字段。

一旦我给了它正确的路径,它就知道该怎么处理这个字段了。

撰写回答