如何摆脱_BaseValue

时间:2013-01-30 10:14:34

标签: python google-app-engine google-cloud-datastore

我正在使用自己的ndb属性子类,因此我可以为它们添加自己的属性。 当我检索存储在ndb中的数据时,我经常(并不总是)在_BaseValue包装器中检索数据。如何避免返回_BaseValues?
目前,当我想使用数据时,我必须将它传递给函数以首先获得b_val。

请求参数

INFO     2013-02-01 08:15:05,834 debug.py:24] discount_application          
INFO     2013-02-01 08:15:05,835 debug.py:24] url_name                      10
INFO     2013-02-01 08:15:05,835 debug.py:24] name                          10%
INFO     2013-02-01 08:15:05,835 debug.py:24] discount.amount               10
INFO     2013-02-01 08:15:05,835 debug.py:24] discount_type                 discount
INFO     2013-02-01 08:15:05,836 debug.py:24] free_text_discount            
INFO     2013-02-01 08:15:05,836 debug.py:24] discount.currency             euro

使用自定义功能

从数据存储区接收的数据
created                       _BaseValue(datetime.datetime(2013, 1, 31, 10, 41, 6, 757020))
updated                       _BaseValue(datetime.datetime(2013, 2, 1, 8, 13, 34, 924218))
name                          _BaseValue('10%')
active                        _BaseValue(True)
name_lower                    _BaseValue('10%')
url_name                      _BaseValue('10_')
discount_type                 _BaseValue('free_text_discount')
discount                      _BaseValue(Discount(amount=0, currency=u'euro'))
free_text_discount            _BaseValue('Krijg nu 10% korting')
discount_application          _BaseValue(' ')

解析请求参数后的数据

created                       2013-01-31 10:41:06.757020
updated                       2013-02-01 08:13:34.924218
name                          u'10%'
active                        True
name_lower                    u'10%'
url_name                      u'10_'
discount_type                 u'discount'
discount                      Discount(amount=1000, currency=u'euro')
free_text_discount            u''
discount_application          u' '

据我所知,数据以我想要或不随机的方式存储。 放置后接收相同实例后的数据如下所示。 put之后的数据显示为discount.discount.amount和discount.discount.currency,而不仅仅是discount.amount和discount.currency

created                       _BaseValue(datetime.datetime(2013, 1, 16, 14, 29, 52, 457230))
updated                       _BaseValue(datetime.datetime(2013, 2, 1, 8, 14, 29, 329138))
name                          _BaseValue('20%')
active                        _BaseValue(True)
name_lower                    _BaseValue('20%')
url_name                      u'20_'
discount_type                 _BaseValue('discount')
discount                      _BaseValue(Discount(discount=Expando(amount=2000L, currency='percent')))
free_text_discount            _BaseValue(' ')
discount_application          _BaseValue('')

动作看起来像这样

# BaseModel has some default properties and inherits from CleanModel
class Action(BaseModel):
    _verbose_name = _("Action")
    max_create_gid = gid.ADMIN
    max_list_gid = gid.ADMIN
    max_delete_gid = gid.ADMIN

    # And some additional irrelevant properties
    # properties is a module containing custom properties,
    # which have some additional properties and functions
    discount = properties.StructuredProperty(Discount,
            html_input_type="small_structure",
            verbose_name=_("Discount"),
            help_message=_("Set a minimum discount of 10%% or € 1,00"),
            max_edit_gid=gid.ADMIN)

    def validate(self, original=None):
        return {}

折扣看起来像这样

# CleanModel has some irrelevant functions and inherits from ndb.Model
class Discount(common_models.CleanModel):
    amount = EuroMoney.amount.update(
            verbose_name=_("Discount"))
    currency = EuroMoney.currency.update(
            choice_dict=cp_dict(EuroMoney.currency._choice_dict,
                                            updates={CURRENCY_PERCENT: "%%"}),
            max_edit_gid=gid.ADMIN)

1 个答案:

答案 0 :(得分:1)

绝不应该使用 _values。应该使用getattr 循环模型属性的示例:

entity = Model(**kwargs)
for name in entity._properties:
    val = getattr(entity, name)