Hi;

For an app that describes Wine Competitions, I have a model that
includes:

class Awardswon(models.Model):
    competition = models.ForeignKey(Competition,
verbose_name="Competition")
    wine = models.ForeignKey(Wine, verbose_name="Wine")
    awardname = models.ForeignKey(Awardname, verbose_name='Award')
    score = models.CharField(blank=True,maxlength=10)


class Wine(models.Model):
    class_number = models.CharField(blank=True, maxlength=100)
    wine_name = models.CharField(blank=True, maxlength=100,
db_index=True)
    varietal = models.ManyToManyField(Varietal)
    <snip>


class Varietal(models.Model):
    varietal = models.CharField(blank=False, maxlength=100)


I'm able to display Wines that have won a specific award, with their
associated varietal information:


def loadWinners(request,award,comp):
    errorText = ""
    if comp and award:
        c = Competition.objects.get(pk=comp)
        aw =
Awardswon.objects.select_related().filter(competition=comp).order_by('-
score')
        aw = aw.filter(awardname=award)
        awn = Awardname.objects.get(pk=award)
        if len(aw)==0:
           errorText = "Nothing Found"
    else:
        aw=[]
        c=[]
        awn=[]
    return render_to_response("winelist.html",{"aw":aw,
"mediaurl":settings.SITE_MEDIA_PREFIX,"error":errorText,"c":c,"awn":awn})

And the view includes

{% for wine_aw in aw %}
        <li><a href="/wine/{{wine.wine.id}}">{{wine_aw.wine}}</a>
({{wine_aw.wine.varietal.all|join:","}}) {{wine_aw.score}}</li>
{% endfor %}

Earlier I'd added StatsMiddleware and followng the author's discussion
learned about select_related. (Thanks).

But, using that tool, it seems that the many-to-many data isn't loaded
in the initial query. That is, django does a query for each awarded
wine to get the related varietals.

Is there a way to load the varietals as part of the initial query?

Thanks.


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

Reply via email to