On Saturday, November 9, 2013 9:28:01 AM UTC-8, Timothy W. Cook wrote:
>
> SOLVED (sort of). 
>
> On Sat, Nov 9, 2013 at 2:56 PM, donarb <don...@nwlink.com <javascript:>> 
> wrote: 
>
> > If the user is a manager, you should be able to just test for membership 
> in 
> > the list: 
> > 
> > {% if user in mgr_list %} 
>
> This made perfect sense after I realized that I was looking at a list 
> of objects and not just the email string returned from the model. But, 
> it didn't work. 
>
> > Another option would be to have a property such as "is_manager' and test 
> for 
> > that in the template. 
> > 
>
> I thought about this but I also need to see if the manager status has 
> expired. So I solved it in this way, though it may not be the most 
> efficient. 
>
> The view now creates an actual list of email addresses of users with 
> active manager status. 
>
> class IndexView(TemplateView): 
>
>     template_name = 'index.html' 
>
>     def get_context_data(self, **kwargs): 
>         context = super(IndexView, self).get_context_data(**kwargs) 
>         mlist = [] 
>         for x in Manager.objects.all(): 
>             if x.expires >= now(): 
>                 print(x.expires) 
>                 mlist.append(x.user.email) 
>         context['mgr_list'] = mlist 
>         return context 
>
>
> Is there a better way?  Will this be particularly slow if I have 1,000 
> managers? 
>
> Thanks, 
> Tim 
>

The better way is to use the database to find the values, rather than doing 
it in Python. Off the top of my head, I'd do something like this:

def get_context_data(self, **kwargs):
    context = super(IndexView, self).get_context_data(**kwargs)
    
    # Get all active managers from the database
    q = Manager.objects.filter(expires__gte=now())
    
    # Create a list of all of their email addresses, the values() and 
values_list() methods
    # can pull out just the fields you want - 'flat' creates a list rather 
than a dict or tuple
    mlist = q.values_list('user__email', flat=True)
    context['mgr_list'] = mlist
    return context

As I said, I haven't tested this and I may be buggy, but you should be able 
to get the idea.


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c04a7b1d-67c2-48d4-9500-2a12453e4bd5%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to