2

This is the what-I-want ( warning: Python noob here, this might be an incorrect way of representing, but I hope you get what I want )

forms = { 
            { 'id' : '1',
              'form' : form_something,
            },
            { 'id'  : '4',
              'form' : form_something2,
            }
        }  

Now my question is how do I create this dictionary in my django view which goes like this, so far->

links = Links.objects.all()
forms = {}

for link in links:
    form = LinkForm(instance = link )
    forms.update({ 'id' : link.id, 'form' : form})
    # which is obviously the wrong way to do it
1
  • your forms is not a valid python dictionary. It is a set of two dictionaries, and you can't do it, because dictionaries are not hashable. Commented Jul 23, 2011 at 9:45

3 Answers 3

2

This will create a list of dictionaries:

links = Links.objects.all()
forms = []
for link in links:
    form = LinkForm(instance = link)
    forms.append({'id': link.id, 'form': form})

If you want to create a dictionary of dictionaries, you have to have keys:

links = Links.objects.all()
forms = {}
for link in links:
    form = LinkForm(instance = link)
    # you need something to use as a key
    forms[key] = {'id': link.id, 'form': form}

Notice I changed where you had spaces in your code to match the standard Python way, but it doesn't really matter.

The form of the nested dictionary would be:

forms = { 
       'key1': { 'id' : '1',
          'form' : form_something,
        },
       'key2': { 'id'  : '4',
          'form' : form_something2
        }
    }  

I added keys and removed the comma from after form_something2.

Sign up to request clarification or add additional context in comments.

Comments

1

I've never worked with django, but in pure python, it'd go something like (you want a list/tuple of forms, not a dictionary of forms):

links = Links.objects.all()
forms = []
for link in links:
  form = LinkForm(instance = link)
  # the comma tells python to treat the contents inside the ( ) as a list
  forms.append({ 'id' : link.id, 'form' : form })


EDIT:

Based on the discussion in the comments, lists are a better option in comparison to tuples in terms of performance. Edited the code above to reflect this change.

6 Comments

The thing is I've got to use this dictionary in django templates, not sure if django template renders lists
This code doesn't work at all... a tuple is immutable, so you can't add to it, and the syntax for an empty tuple is tuple() not (). Also, only use += it when you have multiple items, it's the equivalent of extend. If you have one item, use append.
I've tested the code, and it definitely runs on python 2.6. not sure if its any different in django
but I'll keep your pointers in mind the next time I code in python :)
If you do id(forms) after each time, you'll see you're not adding to it, you're making a copy with one new item every time. With a list you can just add to the same one. I was wrong about () -- I totally forgot that worked.
|
0

i think what you want to do is something like this:

forms = { '1': form_something,
      '4':form_something2,
     }

This way you will look for your forms with their id (which is a common way to find things)

you can do this this way:

forms = {}

for link in Links.objects.all():
    form = LinkForm(instance = link )
    forms[form.id] = form`

Now I am curious why you want to save forms in a dictionary? seems odd to me.

Hope this helps

1 Comment

Django templates take a dictionary of objects to render.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.