views:

81

answers:

2

This is the model:

class Rep(db.Model):
    author = db.UserProperty()
    replist = db.ListProperty(str)
    unique = db.ListProperty(str)
    date = db.DateTimeProperty(auto_now_add=True)

I am writing replist to datastore:

        L = []
        rep = Rep()
        s = self.request.get('sentence')   
        L.append(s)

        rep.replist = L
        rep.put()

and retrieve

mylist = rep.all().fetch(1)

I assume that mylist is a list. How do I print its elements? When I try it I end up with the object; something like [<__main__.Rep object at 0x04593C30>]

Thanks!

EDIT

@Wooble: I use templates too. What I don't understand is that; I print the list L like this:

% for i in range(len(L)):
<tr>
  <td>${L[i]}</td>
</tr>
% endfor

And this works. But the same thing for mylist does not work. And I tried to get the type of mylist with T = type(mylist) that did not work either.

+1  A: 

If you use fetch(1), you'll get a list of 1 element (or None, if there are no entities to fetch).

Generally, to print all of the elements of each entity in a list of entities, you can do something like:

props = Rep.properties().keys()
for myentity in mylist:
     for prop in props:
         print "%s: %s" % (prop, getattr(myentity, prop))

Although most people would just be using a template to display the entities' data in some way.

Wooble
Thanks, I don't understand the first line. But please see the edit above.
Zeynel
A: 

The result of rep.all().fetch(1) is an object. You will need to iterate like this:

{% for i in mylist %}
<tr>
  <td>{{i.author }}</td>
  ...
</tr>
{% endfor %}

If you want to print i.replist (list), you can print it using Django's template function join eg:

{% for i in mylist %}
  <tr>
    <td>{{i.replist|join:"</td><td>" }}</td>
  </tr>
{% endfor %}
Luis Benavides
Thanks. At least now I don't get an error message but I get `None`. And I don't understand why I have to use `i.author`? I thought I stored the contents of string `s` in `L` and `mylist`. Then if I fetch one of these lists; they should contain the contents of `s`. But this is not happening. Any suggestions?
Zeynel
@Luis Benavides: do you know how to get the same effect with Mako templates http://stackoverflow.com/questions/4061673/printing-lists-with-mako-template-django-join-tag
Zeynel