2

I have to fetch multiple rows from database based on some condition. There may be one row in database for matching query or multiple rows. I can use get() for one row but it does not fetch multiple rows.

Here is my code:

value=table.objects.get(col_name=1) then i can access its data as value.col but as i said it gives me error if this query returns more then one row.

I have also tried filter() , value=table.objects.filter(col_name=1) but then i can't access its data as value.col.

So please suggest me some way how i can fetch multiple rows from database and access columns one by one of those rows.

Thanks !!

1
  • 1
    i think i have to use for loop for accessing data if i am using filter , Is i am right ?? Commented Jan 29, 2013 at 9:19

3 Answers 3

3
values = table.objects.filter(col_name=1)
for value in values.all():
    # Do something with value.col
Sign up to request clarification or add additional context in comments.

4 Comments

Can you tell me one thing.....I have updated some fields from update function and it successfully updated the database and i can see the updated fields in back end but from admin dashboard it shows me the previous value not updated value , any idea why??
It is possible your django admin is caching - checkout docs.djangoproject.com/en/dev/topics/cache and try turning off your cache in your settings.py to see if it updates correctly.
it doesn't work ..... I have update the database through handlers...is this the reason or not ??
It's hard for me to diagnose the issue without more information. It may be best to ask a new question on StackOverflow for this new issue with a bit more detail.
1

yes, you must iterate over items from queryset:

for item in value:
    item.col

Comments

0
value=table.objects.filter(col_name=1)

will return a "django.db.models.query.QuerySet" type which can iterated using 'for' loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.