【问题标题】:Django-South introspection rule doesn't workDjango-South 自省规则不起作用
【发布时间】:2011-06-10 14:23:44
【问题描述】:

我正在使用Django 1.2.3South 0.7.3

我正在尝试将我的应用程序(名为 core)转换为使用 Django-South。我有一个正在使用的自定义模型/字段,名为 ImageWithThumbsField。它基本上只是 ol' django.db.models.ImageField 具有一些属性,例如身高、体重等。

在尝试 ./manage.py convert_to_auth core 时,我收到了 South 的 freezing 错误。我不知道为什么,我可能错过了什么......

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

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

有人知道为什么吗?我做错了什么?

【问题讨论】:

    标签: python django migration django-south


    【解决方案1】:

    我明白了! :)

    我改了这个:["^core/.fields/.ImageWithThumbsField",]

    对此:["^lib\.thumbs\.ImageWithThumbsField",]

    这一整行是Django 字段类型 的python 路径的正则表达式(再读一遍,长句)。

    South 偶然发现了在路径 lib.thumbs 中声明的字段名称 ImageWithThumbsField。我给了他一个错误的路径,所以南仍然在绊倒这个领域时不知道该怎么办。

    一旦我给了他正确的路径,它就知道一旦他到达了它就知道如何处理这个领域。

    【讨论】:

    • 还不能接受答案,StackOverflow.com 说我需要等待 2 天才能这样做。
    • 可能这个问题太具体而无法引起太多关注,但只要知道你拯救了我的一天。我需要几个小时才能找到解决方案,这完全成功了。顺便说一句,对我有用的是 ["^myapp.thumbs.ImageWithThumbsField",] 因为我的 thumbs.py 在 myapp 目录中。干杯!
    • 实际上应该是["^lib\.thumbs\.ImageWithThumbsField",])。点“。”在正则表达式中具有特殊含义,因此您必须使用反斜杠对其进行转义。
    • 我使用了@exfizik 格式并且它有效。重要的是获得正确的完整路径!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多