On Thu, Jul 5, 2012 at 4:08 PM, Henrik Genssen <henrik.gens...@miadi.net> wrote:
>>Could you show where and how you are executing the query? If the
>>queryset is a global, and does not go out of scope at the end of the
>>request, then reusing the queryset will not cause the queryset to be
>>re-evaluated.
>>
>>Eg, in this example, categories is a global outside of the view, and
>>once evaluated, will not be re-evaluated just because you used it in a
>>view.
>>
>>categories = Category.objects.all()
>>
>>def index(request):
>>  return render_to_response('index.html': { 'categories': categories })
>>
>>When querysets are executed is documented here:
>>
>>https://docs.djangoproject.com/en/1.4/ref/models/querysets/#when-querysets-are-evaluated
>
>
> yep, global caught me.
> I have a class:
>
> class table(object):
>    queryset = Category.objects.all()
>    column1 = ...
>    column2 = ...
>
>    def render(self):
>       return '<table>...'
>
> in a view I am doing:
>
> tbl = table()
> # somtimes adding filters
> tbl.queryset = tbl.queryset.filter(...)
>
>
> I want to create a html table like "forms" in django
>
> Is it enough to call the all() method on the queryset in my rendering method 
> to get the queryset query the db again?
>
> regards
>
> Henrik
>

Yes, so that queryset is created once, when the class definition is
parsed, and then populated once. Regardless of how many instances of
table that you create, they all get the same object, created when the
class was parsed.

The simplest solution would be to simply assign that queryset inside
the init method, instead of making it a class member, eg:

class table(object):
   column1 = ...
   column2 = ...
   def __init__(self):
     self.queryset = Category.objects.all()

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to