A third alternative that I use very often is to create custom managers. For example all my models have a status field which dictates if the object is shown on main site or not, hence I create a custom manager method called ‘live’ and I can do:
Gallery.objects.live() But what is nice is that IN a template I can access these custom queries without having to write the code in a view. Imagine I want to now access all the Images from a gallery that are live (status enabled), I can write it in a view OR i can call my custom manager like so: {% for image in gallery.objects.live %} ... {% endfor %} Example code here http://dpaste.com/733698/ From: Nikhil Somaru Sent: Tuesday, April 17, 2012 9:24 PM To: django-users@googlegroups.com Subject: Re: [Question] Filter Queryset in a Template Thanks for the replies guys. I realised I was over thinking the matter. I'm just going to do the necessary work in the view for now, get it working, and then worry about optimising later. On 16 April 2012 19:16, Javier Guerra Giraldez <jav...@guerrag.com> wrote: On Mon, Apr 16, 2012 at 6:00 AM, Nikhil Somaru <nsom...@gmail.com> wrote: > If I do the filtering in views.py, I the template would have to make > assumptions about the type of context variables I will be passing it. > > Or am I seeing this the wrong way? i think so. the view is where you manage _what_ is shown, the template is where you manage _how_ is shown. from that, filtering is usually more apropriate to be done on the view. i usually don't worry too much about the template assuming the view works in a specific way, since templates are _so_ rarely reusable (except for inheritance, but i don't think it's your problem) i don't really get what do you mean by "would have to make assumptions about the type of context variables", filtering data (in the view) usually shoudln't change type of variables (querysets are still querysets), it should only change the content of them. -- Javier -- 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 mailto:django-users%2bunsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- Yours, Nikhil Somaru -- 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. Daniel Sokolowski Web Engineer Danols Web Engineering http://webdesign.danols.com/ Office: 613-817-6833 Fax: 613-817-4553 Toll Free: 1-855-5DANOLS Kingston, ON K7L 1H3, Canada Notice of Confidentiality: The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review re-transmission dissemination or other use of or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error please contact the sender immediately by return electronic transmission and then immediately delete this transmission including all attachments without copying distributing or disclosing same. -- 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.