[Django]-Django objects being "non subscriptable" leads me to write redundant code

13👍

setattr is what you’re looking for.

In your case you could do something like this:

for attr, value in info.items():
    setattr(me, attr, value)
👤grncdr

9👍

Why not do the following:

info = {'first_name': 'Artur', 'last_name': 'Sapek'}
user = User(**info)

Of course if you are using contrib.auth‘s User model you’ll need to provide the required fields.

👤shanyu

Leave a comment