Inforian
Inforian

Reputation: 1736

Issues with Fetching the data from database

I can fetch the data like this.

value= mymodel.objects.get(anycondition)

OR

value= mymodel.objects.filter(anycondition)

and can send them to my template with context.

But if I want to select all the data from a table(for all users not only one) as this query does

value= mymodel.objects.all()

and send this value to my template and can see there field by field

e.g.

my table has two fields name and phone no and I use the above query( value= mymodel.objects.all()) now if i want to see all names then i can see that and if i want to see phone no. I can see that too.

I have tried this and it doesn't work and I even I do not know it is possible or not. If it is possible then please let me know how I can do this ?

I hope you understand my question. !!

Thanks in advance

Upvotes: 0

Views: 85

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174614

.all() will return a list of objects that represent the rows in your model. .get() only returns one object. Your template is trying to print the result of all() if it was one object.

This is the same as if you had a list and you wanted to loop through it. In your view you would do:

product = Product_attributes.objects.all()
for i in product:
   print i.size
   print i.color

The equvalent for the template is:

<ul>
{% for i in product %}
   <li>{{ i.size }}</li>
   <li>{{ i.color }}</li>
{% endfor %}
</ul>

Upvotes: 1

Frantz Romain
Frantz Romain

Reputation: 846

Although this question isn't clear it seems like you are having a bit of problem with Field Lookups. It is fairly easy to learn Here is a link to get you started

Upvotes: 0

Related Questions