-1

So I am trying to get into the world of Python and Django. Coming from PHP, many things are similar, but I have a hard time figuring out how to do the following.

In PHP I would do this:

$q = $this->db->query('SELECT * FROM data');

foreach ($q->result_array() as $key => $value) {
    switch ($key) {
        [...]
    }
}

Now, my question is: how can I accomplish something similar in Django / Python? I have this setup:

from index.models import Foo

def index(request):
    datalist = Foo.objects.get(id=1)

And then what?

Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
Diederik
  • 602
  • 9
  • 24

1 Answers1

2

NOTE: The query in your PHP snippet does not even match the one in your Django snippet to start with. The PHP one does a SELECT * FROM data but the Python one does a SELECT * FROM data WHERE id = 1, so I'm gonna go ahead and assume you intended Foo.objects.all() instead of Foo.objects.get(id=1). If you didn't, then please fix your question, and also simply remove the looping part of the code snippets below. Also, you should find a better name for the variable datalist if it's going to contain just one item not a list as the name suggests, which was a source of confusion for me initially.

You haven't really described very clearly what your goal is, but here are a few options that might potentially match your needs:

for obj in datalist:
    print "%s => %s" % (obj.id, obj)

...or if you want to iterate over all key-value pairs of all objects:

for obj in datalist:
    print "OBJECT %s" % obj.id
    for field in Foo._meta.fields:
        print "  %s => %s" % (field.name, getattr(obj, field.name)

...or you can use the django.forms.models.model_to_dict helper to convert the objects to dictionaries:

from django.forms.models import model_to_dict

for obj in datalist:
    print "OBJECT %s" % obj.id
    for key, value in model_to_dict(obj_as_dict).items():
        print "  %s => %s" % (key, value)
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
  • So your first and second solution gives me a "object is not iterable". The third option however works. Thank you so much! – Diederik Oct 07 '13 at 15:24
  • The error is caused by the fact that `datalist` is not an iterable, even though its name suggests it is. I've added a comment at the beginning of the answer about this. – Erik Kaplun Oct 07 '13 at 15:27
  • @DiederikvandenBurger: Read Erik's note to find out why you are getting the `Object is not iterable` error, he clearly explains that your django query will return _one_ result, not a list of results. – hellsgate Oct 07 '13 at 15:28
  • Hmm yeah, so it really is not a list indeed. Sorry for the confusing var name. If I only want to select one iteration, is the third option then the only possible option? – Diederik Oct 07 '13 at 15:35
  • They all work with single items as well, just please **remove the loop** as mentioned in the **NOTE** and only use the loop body. It just program code not Chinese or anything. – Erik Kaplun Oct 07 '13 at 15:37
  • Got it. Sorry for the confusion, though I really do appreciate the help! – Diederik Oct 07 '13 at 15:45
  • Also, I would suggest delving into Python and Django; for Python, see http://learnpythonthehardway.org; for Django, just http://djangoproject.com – Erik Kaplun Oct 07 '13 at 15:48