python - to_python() 永远不会被调用(即使在 __metaclass__ = models.SubfieldBase 的情况下)

标签 python django django-models

前段时间,作为学习Python+Django过程的一部分,我决定为BIT列类型编写一个自定义的MySQL特定的模型字段。不幸的是,我遇到了一个问题。

项目:包含一个“主”应用

“主”应用程序:包含由“python manage.py startapp”创建的所有标准文件,以及 extfields.py

extfields.py内容如下:

from django.db import models
import re
import bitstring

class BitField(models.Field):
    description = 'A class representing a field of type "BIT" (MySQL-specific)'
    __metaclass__ = models.SubfieldBase

    def __init__(self, *args, **kwargs):
        bf_size = int(kwargs.pop('bitfield_size', 0))

        assert bf_size > 0, '%ss must have a positive bitfield_size' % self.__class__.__name__
        self._bf_size = bf_size

        super(BitField, self).__init__(*args, **kwargs)


    def db_type(self):
        return 'BIT(%d)' % self._bf_size


    def to_python(self, value):
        print('to_python starts')

        if isinstance(value, bitstring.BitArray):
            return value    

        value = str(value)

        regex = re.compile('^[01]{%d}$' % self._bf_size)

        assert regex.search(value) == True, 'The value must be a bit string.'

        print('to_python ends')

        return bitstring.BitArray(bin=value)


    def get_db_prep_value(self, value):
        return value.bin

models.py 的内容:

from django.db import models
import extfields

class MessageManager(models.Manager):
    """
    This manager is solely for the Message model. We need to alter the default
    QuerySet so that we'll get the correct values for the attributes bit field   
    """
    def get_query_set(self):
        return super(MessageManager, self).get_query_set().defer(
            'attributes'
        ).extra(
            select={'attributes': 'BIN(attributes)'}
        )


class Message(models.Model):
    attributes = extfields.BitField(bitfield_size=15)

    objects = MessageManager()

当我使用 python shell(通过 python manage.py shell)时,我得到以下信息:

>>> from main import models
>>> m = models.Message.objects.get(pk=1)
>>> m
<Message_Deferred_attributes: Message_Deferred_attributes object>
>>> m.attributes
u'1110001110'
>>> type(m.attributes)
<type 'unicode'>
>>> m.attributes = '1312312'
>>> m.attributes
'1312312'

如您所见,m.attributes 是一个纯字符串,而不是 bitstring.BitArray 实例。

有人可以告诉我哪里出错了吗?

我在 Ubuntu 10.04 上使用 Python 2.6.5。我导入的位串模块是这个:http://code.google.com/p/python-bitstring/ 。 Python-django包版本为1.1.1-2ubuntu1.3。

编辑(回应 emulbreh 的评论):

现在我的 to_python() 定义如下所示:

def to_python(self, value):
        print 'to_python'

        if isinstance(value, bitstring.BitArray):
            return value    

        print type(value)    
        value = str(value)
        print'\n'
        print value
        print '\n'
        print type(value)        

        regex = re.compile('^[01]{%d}$' % self._bf_size)

        assert regex.search(value) == True, 'The value must be a bit string.'
        value = bitstring.BitArray(bin=value)
        print '\n'
        print type(value)
        print 'End of to_python'

        return value

控制台输出为:

此后,将引发断言错误。

最佳答案

您无需在 QuerySet 中执行任何特殊操作即可支持自定义字段。您当前 defer() 您的字段,然后添加一个恰好与您的字段同名的原始 extra(select=) 属性。 如果您只是删除自定义管理器(或 .defer().extra() 调用),您应该没问题。

关于python - to_python() 永远不会被调用(即使在 __metaclass__ = models.SubfieldBase 的情况下),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5318837/

相关文章:

mysql - 在 MySQL 查询中按记录排序并随机混合

python - 使用cv2.HoughLines()的效果是错误的

python - 如何将 NSDictionary 发布到 Django 服务器

python - Django:按计算字段过滤

django - django将对象列表转换为主键列表

python - Django - 格式化日期以从表单更新模型对象

python - 主键和唯一约束上的 Django 模型 unique_together

python - Image.fromarray 改变大小

Python MySQLdb 返回 datetime.date 和 decimal

python - 使用 OpenCV 提高用稀释墨水书写的几乎不可见的旧文本的对比度和质量