Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7165087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:11:00+00:00 2026-05-28T14:11:00+00:00

Django objects aren’t subscriptable meaning if you have user.name you can’t define it with

  • 0

Django objects aren’t “subscriptable” meaning if you have user.name you can’t define it with user['name'] meaning you can’t dynamically load up an object with info.

info = {'first_name': 'Artur', 'last_name': 'Sapek'}

me = User()
for i in info:
    me[i] = info[i]

brings up TypeError: 'User' object is not subscriptable and I find myself writing code like

info = {'first_name': 'Artur', 'last_name': 'Sapek'}

if 'first_name' in info: me.first_name = info['first_name']
if 'last_name' in info: me.first_name = info['last_name']
so on...

(It requires if-statements as well because there is more info and it is not always complete – this isn’t my actual code)

Is there any better way to do this with Django objects, which don’t have the flexibility to use bracket notation like you can with lists, dicts, and strings?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:11:00+00:00Added an answer on May 28, 2026 at 2:11 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

from django.contrib.auth.models import User u = User.objects.get(username='test') user.password u'sha1$c6755$66fc32b05c2be8acc9f75eac3d87d3a88f513802 Is reversing this password encryption
Filtering QuerySets in Django work like the following: Entry.objects.filter(year=2006) How can I use filter
In my django views i have the following def create(request): query=header.objects.filter(id=a)[0] a=query.criteria_set.all() logging.debug(a.details) I
I have a list of objects from a django queryset, e.g. my_list = MyObject.objects.filter(variable=something)
I have an odd problem with Django. I have a set of objects that
As the title says I have a list of Django objects and I want
I need to output objects in Django template, so that each object has its
Is there any way to get the model name of any objects in django
I use Django and I have some objects with ManyToManyFields. I'd like to duplicate
I have a Django project that uses SQLAlchemy to use some legacy ORM objects.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.