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

  • SEARCH
  • Home
  • 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 346723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:12:14+00:00 2026-05-12T11:12:14+00:00

I have this dictionary in my apps model file: TYPE_DICT = ( (1, Shopping

  • 0

I have this dictionary in my apps model file:

TYPE_DICT = (
    ("1", "Shopping list"),
    ("2", "Gift Wishlist"),
    ("3", "test list type"),
    )

model, which uses this dict is this:

class List(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=200)
    type = models.PositiveIntegerField(choices=TYPE_DICT)

I want to re-use it in my views and imported it from apps.models. I am creating a list of dictioneries to use in my view like this:

bunchofdicts = List.objects.filter(user=request.user)
    array = []
    for dict in bunchofdicts:
        ListDict = {'Name':dict.name, 'type':TYPE_DICT[dict.type], 'edit':'placeholder' }
        array.append(ListDict)

When i use this list in my template then it gives me very strange results.
Instead of returning me the type of the list (shopping list) it returns me (‘2’, ‘Gift Wishlist’).

So i can understand what it is doing (in ths case, the dict.type equals 1, and it should return me “shopping list” , but it returns me [1] – second, element in list). What i do not understand, why doing exactly the same thing in python shell gives different results.

doing it the way i do in django ( TYPE_DICT[dict.type] ), works as described above and creates error in python shell. using TYPE_DICT[str(dict.type)] in python shell works just fine, but creates this error in django:

TypeError at /list/

tuple indices must be integers, not str

Request Method:     GET
Request URL:    http://127.0.0.1/list/
Exception Type:     TypeError
Exception Value:    

tuple indices must be integers, not str

Exception Location:     /home/projects/tst/list/views.py in list, line 22
Python Executable:  /usr/bin/python
Python Version:     2.6.2

Perhaps i did something wrong or different in python shell. What i did was:

python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dict = {'1':'shoppinglist', '2':'giftlist','3':'testlist'}
>>> print dict[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1
>>> print dict[str(1)]
shoppinglist
>>> x = 1
>>> print dict[x]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1
>>> print dict[str(x)]
shoppinglist
>>>

so what is wrong here?

Alan

  • 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-12T11:12:14+00:00Added an answer on May 12, 2026 at 11:12 am

    Your TYPE_DICT in your models file isn’t a dictionary: it’s a tuple of tuples.

    You can easily make a dictionary from it though if you want:

    TYPE_DICT_DICT = dict(TYPE_DICT)
    

    then you can use TYPE_DICT_DICT as a real dictionary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 260k
  • Answers 260k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I also had problems with VS 2010 Beta 2 help,… May 13, 2026 at 11:22 am
  • Editorial Team
    Editorial Team added an answer As was mentioned in the post you link to, use… May 13, 2026 at 11:22 am
  • Editorial Team
    Editorial Team added an answer There's no magical python library that will do seasonal adjustments… May 13, 2026 at 11:22 am

Related Questions

Has anyone setup django-paypal? Here is the link to it here ? I have
I have a Dictionary<string,int> that has the potential to contain upwards of 10+ million
I've used HashSet and Dictionary a lot in C#, and found them very fast...
Would pasteboard.numberOfItems (in the code below) ever be greater than 1? How would the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.