PynamoDB icon indicating copy to clipboard operation
PynamoDB copied to clipboard

Dynamic attributes like DynamicMapAttributes and increment/decrement operations.

Open aczire opened this issue 4 years ago • 1 comments

apologies guys for creating an issue for this.

Is it possible to create and use dynamic attributes like DynamicMapAttributes? What I am trying to achieve is, create and set/update/increment/decrement dynamic attributes and its values.

for example,

class Student(Model):
  id = NumberAttribute(null=False)
subject_name = "English"
subject_score = 0
std = Student(id = std_id)
std.<subject_name>= subject_score # since subjects and scores are dynamic.

and then at a later point of time inside a transaction,

Student.<subject_name>.add(10)

I can getaway with putting Sub and Score in a DynamicMapAttribute, but when it comes to incrementing/decrementing it, it seems DynamoDB don't allow doing that for a map element.

I tried creating a DynamicMapAttribute instance as shown below, but still no idea how can I do actions like increment/decrement on the attribute.

class Student(DynamicMapAttribute):
  id = NumberAttribute(null=False)
subject_name = "ABC"
subject_init_score = 0

std = Student(id = 101)
std.__setattr__(subject_name, subject_reset_score)

but then how do I increment/decrement the value witout retrieving it, and adding it (which in turn fails the purpose of ADD)

Any idea?

PS: I cannot do something like this in here, where a_date_time/a_number is static. https://github.com/pynamodb/PynamoDB/blob/58ff97f42f3ed1e546335e6b86f943ca3c23f7f3/pynamodb/attributes.py#L1000

    >>> class MyDynamicMapAttribute(DynamicMapAttribute):
    >>>     a_date_time = UTCDateTimeAttribute()  # raw map attributes cannot serialize/deserialize datetime values
    >>>
    >>> dynamic_map = MyDynamicMapAttribute()
    >>> dynamic_map.a_date_time = datetime.utcnow()
    >>> dynamic_map.a_number = 5

aczire avatar Aug 25 '21 19:08 aczire

I'm also interested in this, see https://github.com/pynamodb/PynamoDB/issues/1008. Did you ever find a solution?

Seluj78 avatar Dec 22 '21 14:12 Seluj78