from django import template
from partner_feeds.models import Partner
register = template.Library()

@register.assignment_tag
def get_partners(*args):
    partners = []
    for name in args:
        try:
            partner = Partner.objects.get(name=name)
        except Partner.DoesNotExist:
            continue
        partner.posts = partner.post_set.all().order_by('-date')
        partners.append(partner)
    return partners
from django import template
from partner_feeds.models import Partner, Post
register = template.Library()


@register.assignment_tag
def get_partners(*partner_names):
    """
    Given a list of partner names, return those partners with posts attached to
    them in the order that they were passed to this function

    """
    partners = list(Partner.objects.filter(name__in=partner_names))
    for partner in partners:
        partner.posts = Post.objects.filter(partner=partner)
    partners.sort(key=lambda p: partner_names.index(p.name))
    return partners

Update `get_partners` assignment tag to reduce the number of queries
Update `get_partners` assignment tag to reduce the number of queries

Maintains the same interface so no other changes should be required
  from django import template
- from partner_feeds.models import Partner
+ from partner_feeds.models import Partner, Post
  register = template.Library()
  
+ 
  @register.assignment_tag
- def get_partners(*args):
+ def get_partners(*partner_names):
-     partners = []
-     for name in args:
-         try:
-             partner = Partner.objects.get(name=name)
-         except Partner.DoesNotExist:
-             continue
-         partner.posts = partner.post_set.all().order_by('-date')
-         partners.append(partner)
+     """
+     Given a list of partner names, return those partners with posts attached to
+     them in the order that they were passed to this function
+ 
+     """
+     partners = list(Partner.objects.filter(name__in=partner_names))
+     for partner in partners:
+         partner.posts = Post.objects.filter(partner=partner)
+     partners.sort(key=lambda p: partner_names.index(p.name))
      return partners
+ 
Update `get_partners` assignment tag to reduce the number of queries
## Code Before:
from django import template
from partner_feeds.models import Partner
register = template.Library()

@register.assignment_tag
def get_partners(*args):
    partners = []
    for name in args:
        try:
            partner = Partner.objects.get(name=name)
        except Partner.DoesNotExist:
            continue
        partner.posts = partner.post_set.all().order_by('-date')
        partners.append(partner)
    return partners
## Instruction:
Update `get_partners` assignment tag to reduce the number of queries
## Code After:
from django import template
from partner_feeds.models import Partner, Post
register = template.Library()


@register.assignment_tag
def get_partners(*partner_names):
    """
    Given a list of partner names, return those partners with posts attached to
    them in the order that they were passed to this function

    """
    partners = list(Partner.objects.filter(name__in=partner_names))
    for partner in partners:
        partner.posts = Post.objects.filter(partner=partner)
    partners.sort(key=lambda p: partner_names.index(p.name))
    return partners
