Search code examples
pythongoogle-app-enginegoogle-cloud-datastore

How to get rid of _BaseValue


I'm using my own subclasses of ndb properties so I can add my own properties to them. When I retrieve data stored in the ndb, I often (not always) retrieve the data in a _BaseValue wrapper. How can I avoid the returning of _BaseValues?
Currently when I want to use the data I have to pass it to a function to get the b_val first.

Request arguments

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

Data received from datastore using printed using custom function

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

Data after parsing the request arguments

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

As far as I can tell the way the data is stored the way I want or not randomly. Data after receiving the same instance after putting is shown below. Also the data after putting is shown as discount.discount.amount and discount.discount.currency instead of just discount.amount and 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('')

Action looks like this

# 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 {}

And discount looks like this

# 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)

Solution

  • _values should never be used. getattr should be used instead.
    An example for looping through properties of a Model:

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