What does fetch() fetch in GAE?
This is a follow up to my other question.
I thought that
mylist = list(Rep().all().fetch(50))
makes mylist
a list. But when I try to get its length I get the message
self.response.out.write(len(P))
TypeError: object of type 'Rep' has no len()
Can anyone explain what I am doing wrong?
Rep().replist = L
Rep().put()
mylis开发者_StackOverflow中文版t = list(Rep().all().fetch(50))
P = mylist.pop()
self.response.out.write(len(P))
UPDATE
As a reference for others who may encounter the same problem; I post the following table which was very helpful to me. (The original here)
Rep().........................Rep object
Rep.all().....................Query object
list(Rep.all())...............List of Rep objects.
list(Rep.all())[0]............A single Rep object
list(Rep.all())[0].replist....A list
Thanks for all the answers.
Instead of this:
Rep().replist = L
Rep().put()
mylist = list(Rep().all().fetch(50))
P = mylist.pop()
self.response.out.write(len(P))
Try something like this:
r = Rep()
r.replist = L
r.put()
mylist = Rep.all().fetch(50)
P = mylist.pop()
self.response.out.write(len(P.replist))
This code of yours:
Rep().replist = L
Rep().put()
Is creating a Rep
instance, then assigning its replist
to L
. Then it's creating another Rep
, and calling put()
on it. So the one you are writing to the datastore is a blank Rep
- it won't have your list.
In this code:
mylist = list(Rep().all().fetch(50))
You are calling all()
on an instance of Rep
- you should instead call it directly on the class. Also you don't need to wrap the results in list()
, as fetch()
already returns a list.
Then below where you have this:
self.response.out.write(len(P))
You are trying to get the length of P
(which is a Rep
), not the length of P
's replist
.
Update:
In response to the first comment:
In this code:
r = Rep()
The Rep()
is creating an instance of a Rep
. The r =
is then assigning that instance to the name r
. So now the name r
refers to that instance of a Rep
.
Then in this code:
r.replist = L
It is assigning the replist property of r
to refer to the list L
.
You are correct, instead of those two lines you can do this:
r = Rep(replist = L)
What this does is pass L
to the __init__
function of Rep
, with the argument name replist
. The __init__
function of Rep
is inherited from the db.Model
base class. This function assigns the value of any arguments provided to a property of the same name on the model. So in this case, it assigns L
to the replist
property. So it has the same effect as the original two lines of code, but it works a bit differently.
In response to the second comment:
The = operator in Python means assignment, which is not the same as mathematical equivalence.
So this code:
r = Rep()
Does not mean that r
is now equivalent to Rep()
, and that you can now use r
and Rep()
to mean the same thing.
What it means is that r
is now equal to the result of Rep()
. What Rep()
does is allocate a new instance of a Rep
. So that makes r
a reference to a new Rep
. To refer to that same instance later, you therefore need to use r
, not Rep()
(which would allocate a new instance each time you call it).
if you want to print the number of elements of replist property.
self.response.write(len(P.replist));
Hope i can help you :p
Try this:
self.response.out.write(len(mylist))
精彩评论