python - Whats the difference iterating over the queryset in these two ways? -
i using django building project , came across these 2 ways.
one way :
users = users.objects.filter(is_verified=true) user in users: print user.user.id
and second way :
users = users.objects.filter(is_verified=true) user in users: print user.id
at first thought first way wont work working , quite slow second way.
the first 1 looking @ user
property inside of individual users
object.
basically first 1 doing:
print users[x].user.id
the second doing:
print users[x].id
where x
current index in for
loop.
if there user property inside of users
object, first correct. otherwise you'd want access id
property of individual users
object.
an example of users
class first scenario be:
class users(object): user = user()
an example of users
class second senario be:
class users(object): id = 0
i think what's happening when accessing user
property on users
object, may querying database again. if can include users
class definition easier understand root problem.
Comments
Post a Comment