Monday, 15 April 2013

python - Django: access attributes of queryset using index notation not dot notation? -


When I try to do this in Dijongo:

  creatures = creatures Bob (Code = c) field = ['name', 'linen_type', 'age'] # In reality, there are more in organisms: in the field F: print o [f]  

I get this error:

  There is no attribute in the type 'organism' object; '__getitem__'  

Is there any way that I Each result in the query without using?

When you use [] notation, internally, The dragon will try to apply the __getitem __ method to that object. Since the living object object does not define that method, it fails with that error.

Instead, you can use the function, such as

  print getattr (o, f)  

you use Can also specify a default value if the object is not present in the object, such as << p>

  print (c), ("f", "{}" {} is not present in Format (F, O))  

No comments:

Post a Comment